4

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.

3 Answers 3

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

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

2 Comments

This is a jsonld data structe. It needs a @context to be defined, as well as several other tokens defined in json-ld.
If you are interested in it you can read it in the Syntax Tokens and Keywords specification.
1

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']);`

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.