Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

turn typescript object into json string

I'm trying to initialize an object in typescript which requires a JSON string for the "options" parameter. To be precise it is the object here. The options parameter is required to be a JSON string and not an object for initializing the dijit.

Is there a way to create a JSON string from a typescript object without it being a manual process?

Please DO NOT link any questions which don't specifically say "TypeScript" as this question specifically relates to TypeScript. While a derivative of JavaScript the way that you write code is different and therefore this is the only post asking this question currently relating to TypeScript.

like image 774
user1567453 Avatar asked Feb 12 '16 00:02

user1567453


People also ask

How do I convert a JSON object to a string?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

How do I create a JSON string in typescript?

var obj = new Object(); obj.name = "Raj"; obj. age = 32; obj. married = false; //convert object to json string var string = JSON. stringify(obj); //convert string to Json Object console.

How do I Stringify an object in typescript?

Just use JSON. stringify(object) . It's built into Javascript and can therefore also be used within Typescript. Probably going to choose this one as the answer since it explains why I can use JSON.

How can convert an object to JSON in JS?

Answer: Use the JSON. stringify() Method You can use the JSON. stringify() method to easily convert a JavaScript object a JSON string.


1 Answers

Just use JSON.stringify(object). It's built into Javascript and can therefore also be used within Typescript.

like image 183
Luka Jacobowitz Avatar answered Sep 20 '22 01:09

Luka Jacobowitz