Reputation: 141
Have been working with NodeJS (Javascript) for a while and I like to know why stuff is happening. I was hoping someone could answer this.
When consuming some kind of API that returns JSON, I'm used to utilize JSON.parse() to convert JSON to object.
Why is it that sometimes Javascript just automatically converts some JSON to Object and sometimes not? This mean it´s not consistent when JSON.parse will be needed?
Best regards, Christian
Upvotes: 0
Views: 1891
Reputation: 2727
JSON.parse takes a string as an argument. The string is an object on JSON, but converted to string. Thats why you need to parse it.
BTW: JSON mean Javascript Object Notation and if the incoming object is in JSON it is already an javascript object ready to use.
e.g. try out import myObject from 'my-object.json'
Regarding APIs: It depends on the library you are using. Normally all libraries returning the data already in JSON format. What library do you use?
Upvotes: 2