Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript - flat a JSON model using Class-transformer

I am trying to use the class transformer in typescript (node.js): https://github.com/typestack/class-transformer

I would like to flat a JSON using only 1 model , 1 Dto that will go to the DB

if I have the following json :

{
 "data":{"test":"123"}
 "name":"x"
}

the DB service should receive the following object:

 {
   "test":"123",
   "name":"x"
 }

This what I tried to define but it's not working :

@Expose()
export class Dto{
  name:string,
  @Expose({name: "data.test"})
  test: string
}

the result on the test field is undefined how can I achieve that? I don't want to create a "mediator" model

like image 366
Tuz Avatar asked Oct 20 '25 15:10

Tuz


2 Answers

according to doc make

@Expose()
@Transform(({obj}) => obj.data.test)
test: string;
like image 59
Amr Abdalrahman Avatar answered Oct 22 '25 06:10

Amr Abdalrahman


you are trying to flatten a JSON object right? I think you should do it manually. like this

function Flatten(myObj){
    const keys = Object.keys(myObj);
    const newObj = {};
    keys.forEach((key)=>{
        if (typeof myObj[key] != "object"){
            newObj[key] = myObj[key];
        }else{
            const nestedObj = Flatten(myObj[key]);
            Object.assign(newObj,nestedObj);
        }
    });
    return newObj;
}

NOTES:
This is not a TypeScript function but you can turn it into one.
This might not work if the nested Object is an array. you have to do extra checks.

Edit:- if the nested object is array change the if Condition to this

   if (typeof myObj[key] == "object" && !Array.isArray(myObj[key])){
        const nestedObj = Flatten(myObj[key]);
        Object.assign(newObj,nestedObj);
    }else{
       
        newObj[key] = myObj[key];
    }
like image 45
Ahmed Magdy Avatar answered Oct 22 '25 06:10

Ahmed Magdy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!