Mayukh Roy
Mayukh Roy

Reputation: 1815

JSON parsing in javascript for json-ld

Trying to parse a JSON from json-ld

Here is the JSON below:

{
  "@context": "http://json-ld.org/contexts/person.jsonld",
  "@id": "http://dbpedia.org/resource/John_Lennon",
  "name": "John Lennon",
  "born": "1940-10-09",
  "spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
}

So I am trying to do this:

var jsonData= {
  "@context": "http://json-ld.org/contexts/person.jsonld",
  "@id": "http://dbpedia.org/resource/John_Lennon",
  "name": "John Lennon",
  "born": "1940-10-09",
  "spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
};

console.log(jsonData.@context);// Error:Uncaught SyntaxError: Invalid or unexpected token
console.log(jsonData.name);// John Lenon

How do i parse the @context then? Please suggest.

Upvotes: 4

Views: 4238

Answers (3)

Aatif Bandey
Aatif Bandey

Reputation: 1145

You can parse it as:

var jsonData = {
    "@context": "http://json-ld.org/contexts/person.jsonld",
    "@id": "http://dbpedia.org/resource/John_Lennon",
    "name": "John Lennon",
    "born": "1940-10-09",
    "spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
};

console.log(jsonData['@context']);`

Upvotes: 1

Daga Arihant
Daga Arihant

Reputation: 484

Please use

console.log(jsonData['@id']).

Not only this also you cannot use a Javascript variable name starting with @.

you can refer to this for javascript variable naming convention. https://mathiasbynens.be/notes/javascript-identifiers

Upvotes: 3

WangYudong
WangYudong

Reputation: 4423

console.log(jsonData['@context']);

More about Javascript Property accessors: dot notation and bracket notation.

Upvotes: 10

Related Questions