Search code examples
node.jstypescriptclass-transformer

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


Solution

  • 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];
        }