JSON auto parsed to object in Javascript

2848 views javascript

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

answered question

What are you using to call the API, XHR? fetch? built-in node http[s]? request-promise? Each library can implement their own semantics. It's a trade-off between convenience and flexibility.

1 Answer


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?

posted this

Have an answer?


Please login first before posting an answer.