-1

I'm retrieving a very large data from our API back-end. I need to convert the JSON object to a typescript object. I've tried doing JSON.parse(JSON.stringify(result)); but unfortunately, the model created from this has no methods included. How can I convert this JSON object to a typescript class? Could you please suggest any npm plugin that I can use?

result => {
      this.app = JSON.parse(JSON.stringify(result));
      resolve(true);
    }

I have a gigantic model wherein it involves nested object and polymorphism. I was hoping that I can use a npm plugin to help me

  • 1
    Possible duplicate of [How do I initialize a typescript object with a JSON object](https://stackoverflow.com/questions/22885995/how-do-i-initialize-a-typescript-object-with-a-json-object) – jcalz Oct 14 '17 at 21:17
  • Possible duplicate of [TypeScript JSON string to class](https://stackoverflow.com/questions/40171620/typescript-json-string-to-class) – jcalz Oct 14 '17 at 21:18

1 Answers1

0

The object from JSON.parse is a plain Object.

You could create a class and use the object as an argument for the constructor and copy the properties to the new object.

You could also try using: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf

mabs
  • 987
  • 1
  • 10
  • 17