Access JSON values

,

JSON is a JavaScript Object Notation it works as a data structure for APIs and readable for humans and you can parse and serialize.

Example

{
  "documents": [{
    "company": [
      {
        "name": "MFoda-Tech",
        "logo": "/var/data/logo.png",
        "field": "software",
      },
      { "location": "Cairo" }
    ],
    "message": {
      "message": "Hello MFoda-Tech"
    }

  }]
}

use JSON.parse() to convert a JSON to JavaScript object

var data = JSON.Parse({
  "documents": [{
    "company": [
      {
        "name": "MFoda-Tech",
        "logo": "/var/data/logo.png",
        "field": "software",
      },
      { "location": "Cairo" }
    ],
    "message": {
      "message": "Hello MFoda-Tech"
    }

  }]
})

console.log(JSON.stringify(data));

The output

{"documents":[{"company":[{"name":"MFoda-Tech","logo":"/var/data/logo.png","field":"software"},{"location":"Cairo"}],"message":{"message":"Hello MFoda-Tech"}}]} 

If you want to access a company values

var result = data.documents[0].company;
console.log(JSON.stringify(result));

The output

[{"name":"MFoda-Tech","logo":"/var/data/logo.png","field":"software"},{"location":"Cairo"}]

Acess to values in array company

var result = data.documents[0].company[0].name;
console.log(JSON.stringify(result));

The output

"MFoda-Tech"

Access to location

var result = data.documents[0].company[1].location;
console.log(JSON.stringify(result));
"Cairo"

and you can modify location to be Giza

var result = data.documents[0].company[1].location = "Giza";

The output

"Giza"
1 Like