Languages
[Edit]
EN

JavaScript - parse json

6 points
Created by:
Root-ssh
178180

In this short article we would like to show how to convert JSON to object in JavaScript.

The article is focsed on JSON.parse() method, but at the beginning we try to present basic definitions in simple way.

Note: scroll to below exmple if you know well definitions.

Simple definitions:

TermDescription

JSON

Approach to preset JavaScript object as text (can be stored as string type in JavaScript VM).

So:

  • when we create some object in source code using var object = { ... } we can call { ... } part as JSON - it will be converted automatically to object after program is run / compiled.
    We can say: object was created using JavaScript Object Notation,
  • when we do fetch(...) / AJAX request and we recive response with Content-Type: application/json.
    We can say:  we recived JSON that should be parsed to object.
  • when we store { ... } in text file or send as message we can call it JSON.
objectStored in memory data strucure.
JSON to string conversion

Operation that deserilises / converts text to object.

We can do it in few ways:

  • with JSON.parse('{}') method that parses JSON to object,
  • with built-in parse method in fetch(...) result:
    var object = fetchResult.json()
    Method json() returns object created from JSON.
  • setting json value for responseType property during using XMLHttpRequest / XHR that makes response conversion to object.

Quick solution:

Alternative titles

  1. JavaScript - convert json to object
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - JSON

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join