JavaScript “Object to String” the usage of JSON.stringify()


whereas growing a JavaScript utility, you’re going to now and again want to serialize your knowledge into a undeniable string. This will also be helpful for issues like: Storing object information in a database Outputting object data to the console for debugging Sending object information over AJAX or to an API the straightforward syntax for changing an object to a string is: 1 JSON.stringify(worth) the whole syntax is: 1 JSON.stringify(value[, replacer[, space]]) word: For the purposes of this article, every time we are saying “object” we mean “object, array, or worth.” We had it the wrong way around at the beginning, but we speedy realized this changed into annoying to learn. Examples of Going From an Object to a String Let’s see some easy examples. notice that the whole string will get double rates and all of the knowledge in the string will get escaped if wanted. 1 2 three four JSON.stringify(“foo bar”); // “”foo bar”” JSON.stringify([“foo”, “bar”]); // “[“foo”,”bar”]” JSON.stringify(); // ” JSON.stringify(‘foo’:authentic, ‘baz’:false); // “”foo”:proper,”baz”:false” that is all lovely easy. It’s extremely helpful for debugging purposes and for seeing all of the data in your JavaScript objects and arrays. There are two different parameters that can be enter into the JSON.stringify() function and we will be able to go over these under. they are great to learn about but are used sometimes. Filtering the Output For more regulate over the stringify output, you can pass an array of string or number fields with a view to act as a spread filter for the stringify output: 1 2 3 var obj = title: “foo”, identity: 1, age: 45; JSON.stringify(obj, [‘name’, ‘id’]); // outputs: “name”:”foo”,”identification”:1″ you must additionally define a replacer perform to get extra keep watch over over the resulting string output. 1 2 3 four 5 6 7 eight 9 10 perform replacer(key, value)   if (key === “age”)     return price + ‘ years’;      return value; //finish replacer   var obj = title: “foo”, identification: 1, age: forty five; JSON.stringify(obj, replacer); // outputs: “”title”:”foo”,”identity”:1,”age”:”45 years”” any other manipulation of the key-value pairs is that you can imagine with the replacer perform regulate. Indent the Output You’ll regularly wish to indent the stringify output for higher readability. you can do so the use of quite a lot of spaces or a string comparable to a TAB character. Most times, default indentation will work wonderful, however if you would like to vary it, this is how to take action. Indenting with TAB Let’s indent the following JSON information with a TAB character: t 1 JSON.stringify( ‘foo’:1, ‘bar’:2, ‘baz’: ‘quux’:three , null, ‘t’); this will likely output: 1 2 3 four 5 6 7 ”         “foo”: 1,         “bar”: 2,         “baz”:                 “quux”: three          ” we will see that every object depth will get a further TAB indentation. be aware also that the replacer argument is “null” here as we do not use it. Indenting with spaces Let’s indent the identical object with four spaces: 1 JSON.stringify( ‘foo’:1, ‘bar’:2, ‘baz’: ‘quux’:three , null, 2); The output: 1 2 three four 5 6 7 ”   “foo”: 1,   “bar”: 2,   “baz”:     “quux”: 3    ” Going From a String to an Object What if you want to take a string and convert it again into an object or an array? JSON.parse() parses a string as JSON, so it will take in a string price and output a JSON value. Ergo, it’s the other of JSON.stringify(). That’s enough stringification enjoyable for now. Manipulating JSON into your string layout is beautiful simple to do, however as at all times, for those who’re nonetheless stuck, drop us a line under.


Leave A Reply