turn typescript object into json string

JavascriptJsonTypescriptEsri

Javascript Problem Overview


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 https://developers.arcgis.com/javascript/jsapi/editor-amd.html">here</a>;. 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.

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

TS gets compiled to JS which then executed. Therefore you have access to all of the objects in the JS runtime. One of those objects is the JSON object. This contains the following methods:

  • JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.
  • JSON.stringify() method converts a JavaScript object or value to a JSON string.

Example:

const jsonString = '{"employee":{ "name":"John", "age":30, "city":"New York" }}';


const JSobj = JSON.parse(jsonString);

console.log(JSobj);
console.log(typeof JSobj);

const JSON_string = JSON.stringify(JSobj);

console.log(JSON_string);
console.log(typeof JSON_string);

Solution 3 - Javascript

You can use the standard JSON object, available in Javascript:

var a: any = {};
a.x = 10;
a.y='hello';
var jsonString = JSON.stringify(a);

Solution 4 - Javascript

Be careful when using these JSON.(parse/stringify) methods. I did the same with complex objects and it turned out that an embedded array with some more objects had the same values for all other entities in the object tree I was serializing.

const temp = [];
const t = {
  name: "name",
  etc: [{
    a: 0
  }],
};
for (let i = 0; i < 3; i++) {
  const bla = Object.assign({}, t);
  bla.name = bla.name + i;
  bla.etc[0].a = i;
  temp.push(bla);
}

console.log(JSON.stringify(temp));

Solution 5 - Javascript

If you're using fs-extra, you can skip the JSON.stringify part with the writeJson function:

const fsExtra = require('fs-extra');

fsExtra.writeJson('./package.json', {name: 'fs-extra'})
.then(() => {
  console.log('success!')
})
.catch(err => {
  console.error(err)
})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1567453View Question on Stackoverflow
Solution 1 - JavascriptLuka JacobowitzView Answer on Stackoverflow
Solution 2 - JavascriptWillem van der VeenView Answer on Stackoverflow
Solution 3 - JavascriptGiovanni P.View Answer on Stackoverflow
Solution 4 - JavascriptOliver GroßView Answer on Stackoverflow
Solution 5 - JavascriptPaul Razvan BergView Answer on Stackoverflow