Utilizing JSON parsing for JSON-LD in JavaScript

Tried parsing a JSON data obtained from json-ld

Here's the JSON snippet for reference:

{
  "@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"
}

I attempted the following:

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 can I correctly parse the @context value? Any suggestions are welcome.

Answer №1

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

To dive deeper into Javascript, check out this Resource on Property accessors: covering both dot notation and bracket notation.

Answer №2

Make sure to utilize

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

Additionally, avoid using a JavaScript variable name that begins with @.

For guidance on JavaScript variable naming conventions, you can check out this resource:

Answer №3

If you want to extract information from it, you can do so with the following code:

var data = {
    "@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(data['@context']);`

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue: The system is unable to locate the "moduleIntro.js" module while executing the "http" command

I recently embarked on a journey to learn Node.js and decided to experiment with the 'http' module. Here is a snippet of the code I tried to run: var http = require('http'); http.createServer(function (req, res) { res.write('H ...

What is the process for assigning a JSONArray to a model class?

Is there a way to assign a JSON array to a model class? I've been attempting to do so, but keep encountering errors. Can someone provide some guidance? offerlines = new ArrayList<Offerlinemodel>(); Offertextlistmodel olms = new Offertextlistmod ...

Creating a basic Angular 1 application from scratch

Currently, I am in the process of setting up a small application with Angular 1 to enhance my skills (yes, we are still using version 1 at my workplace and I want to familiarize myself with it). Most of the application is working fine, but as soon as I in ...

What is the best way to create an arrow that tracks the browser window, but only goes as far as the end of its designated container

Currently, I am facing a challenge in implementing a scroll down arrow. While this is typically a simple task, it has proven to be a bit more complex this time around. The complexity arises from the fact that my customer desires a homepage with an image of ...

How can you write a parameterless function in Ramda using a point-free style?

Take a look at this functioning code snippet: var randNum = x => () => Math.floor(x*Math.random()); var random10 = randNum(10) times(random10, 10) // => [6, 3, 7, 0, 9, 1, 7, 2, 6, 0] The function randNum creates a random number generator that wi ...

The SQL Server encountered an issue while trying to convert a data type from nvarchar to decimal

When processing the code below, a JSON message is being opened and the value associated with the '$.disgust' field on the JSON message is being inserted into the post_metric_score table. Everything seems to be working fine, except when the value ...

Retrieve the accurate file name and line number from the stack: Error object in a JavaScript React Typescript application

My React application with TypeScript is currently running on localhost. I have implemented a try...catch block in my code to handle errors thrown by child components. I am trying to display the source of the error (such as file name, method, line number, ...

Manipulating Typescript JSON: Appending child attributes and showcasing them alongside the parent item attributes

Here is a JSON data that needs to be processed: var oldArr = [{ "careerLevel": "Associate", "careerLevels": [ { "201609": 21, "201610": 22, "careerID": "10000120" }, { "201609": 31, "201610": 32, ...

What is the best way to extract "true" values from an array and store them in a new array during iteration?

I am currently enrolled in a Codecademy course and I am facing a roadblock. My main goal is to gain a solid grasp of JavaScript. The current task at hand is as follows: "There is an array of unnecessary words. Your goal is to iterate over the array and fi ...

What are some ways to improve performance in JavaScript?

Can you help me determine which approach would be more efficient - using native functions of the language that involve 2 iterations or a simple for loop? The goal is to locate the index in an array of objects where the property filterId matches a specific ...

"Looking for a solution to the ESLint error related to 'no-unused-var' and Google Maps integration. How can I effectively resolve

I'm struggling with what seems to be a simple problem I tried adding /* export myMap */ or /* global myMap */ at the beginning of the script but I keep getting errors Code HTML <h1>My First Google Map</h1> <div id="googleMap" ...

Using Ruby on Rails to incorporate AJAX for posting and commenting functionality

Could use some assistance with implementing AJAX into my project. It seems like it should be a simple task, but I've been struggling with it for days. My goal is to have new comments appear without the need to reload the page. Below are references to ...

What steps should I take to manually split code in preact?

In my project, I am looking to manually implement code-splitting with preact. While preact already handles code splitting for routes, I want to take control of it. The scenario is that I am creating a tool where users can customize widgets on a dashboard. ...

A Step-By-Step Guide on Displaying Two Forms with Two Buttons in HTML

My goal is to develop a functionality on a webpage where users can create keys in a database. The user will need to select between two buttons, each button representing a different form to be displayed. The challenge I am facing is being able to utilize o ...

Interacting with JIRA using Java for handling requests and responses through its REST

This is my first question on stackoverflow. I am trying to implement a post request (using an inputBean/pojo class for necessary parameters) and receive a response (using an outputBean/pojo class to map the json response) using the Jira REST API. Currently ...

Accessing images from subreddit using reddit API

Seeking inspiration from reddit API, I am interested in extracting images from a specific subreddit (http://www.reddit.com/r/VillagePorn) and showcasing them on a webpage. I have observed similar functionality on other websites (specifically, imgur.com/r/* ...

Generating Tree Structure Object Automatically from Collection using Keys

I am looking to automatically generate a complex Tree structure from a set of objects, with the levels of the tree determined by a list of keys. For example, my collection could consist of items like [{a_id: '1', a_name: '1-name', b_id ...

Sliding an image from the left side to the right, and then repeating the movement from left to right

I am currently working on a CSS effect for some images. My goal is to have an image appear from the left side, move towards the right side, then reappear from the left and repeat this cycle. I managed to achieve this using the code below: @keyframes sli ...

Tips for displaying or concealing data in table cells in Google Charts

Using a Google charts table to display server exceptions, but the errors are too long for cells. How can I show only an excerpt of error messages in each cell with a + expansion sign for full details in a modal box on click? I have successfully implement ...

Is there a benefit to using the Angular LocalStorageModule alongside angular-cache?

To configure the angular-cache, follow this setup: app.service('myService', function ($angularCacheFactory) { // This cache will synchronize with localStorage if available. Upon each app load, it will attempt to retrieve any previously save ...