Understanding json

I’m confused to understand json in working with json module, in Active learning: Working through a JSON example we use response.type = ‘json’, Is server send us json like this ‘{“name”: “Bob”}’ and response.type = ‘json’ convert it to javascript object {name: ‘Bob’} ? so why in next example we use response = ‘text’ and then we use JSON.parse.

Hi @Junaid_Arshad! Both effectively do the same thing — in one case you are receiving JSON directly (well, a JavaScript object based on it), and in the other you are receiving text and then converting it to a JavaScript object.

I wanted to show that both were possible.

So i don’t need receive data as text request.responeType = ‘text’ and then convert it to javascript object with JSON.parse, i simply do request.responseType = ‘json’ it will automatically convert the json into javascript object.

But sometimes we aren’t so lucky — sometimes we receive a raw JSON string, what is difference between json and raw json string ?


The difference is that type = json is represented in JavaScript as an object, e.g.

  name: "Chris"

Whereas type = text is represented as a text string:

'{name: "Chris"}'