Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between json string and parsed json string

Tags:

json

parsing

what is the difference between json string and parsed json string? for eg in javascript suppose i have a string in the json format say [{},{}]

parsing this string will also produce the same thing.

So why do we need to parse?

like image 753
May13ank Avatar asked Jun 27 '12 06:06

May13ank


People also ask

What is difference between JSON string and string?

The difference is transfer format. JSON is only the 'Notation' of a JavaScript Object, it is not actually the JavaScript 'object-literal' itself. So as the data is received in JSON, it is just a string to be interpreted, evaluated, parsed, in order to become an actual JavaScript 'Object-Literal.

What happens if you JSON parse a string?

JSON.parse() parses a JSON string according to the JSON grammar, then evaluates the string as if it's a JavaScript expression. The only instance where a piece of JSON text represents a different value from the same JavaScript expression is when dealing with the "__proto__" key — see Object literal syntax vs. JSON.

What is the difference between JSON string and JSON object?

JSON is a string format. The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.

What does it mean to parse JSON?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON.


1 Answers

It's just serialization/deserialization.

In Javscript code you normally work with the object, as that lets you easily get its properties, etc, while a JSON string doesn't do you much good.

var jsonobj = { "arr": [ 5, 2 ], "str": "foo" };
console.log(jsonobj.arr[1] + jsonobj.str);
// 2foo

var jsonstr = JSON.stringify(jsonobj);
// cannot do much with this

To send it to the server via an Ajax call, though, you need to serialize (stringify) it first. Likewise, you need to deserialize (parse) from a string into an object when receiving JSON back from the server.

like image 192
McGarnagle Avatar answered Sep 18 '22 23:09

McGarnagle