Getting JSON element with a hyphenated name in JavaScript is simple. Just use bracket notation and wrap the name of the element in

I am working with a JSON object that contains the following values.

{
   "articles":[
      {
         "paper-id":"id",
         "paper-id-type":"type",
         "title":"title",
         "url":"url",
         "abstract":"abs",
         "date":"date",
         "publication-forum":"forum",
         "publication-forum-type":"type",
         "authors":"auth",
         "keywords":"key1,key2"
      }
   }

My goal was to access and manipulate these results using JavaScript. I initialized an array and stored the results in it.

The structure of the 'articles' array object is as follows;

abstract: "xxx"
authors: "yyy"
date: "1111"
keywords: "key1, key2"
paper-id: "abc"
paper-id-type: "xxx"
publication-forum: "yyy"
publication-forum-type: "zzz"
title: "www"
url: "url"

While trying to retrieve each value from these elements, I encountered a challenge due to the presence of "-" character in some keys. For example, when attempting to obtain the paper-id information;

articles[0]["paper-id"]

This resulted in an error message stating

[Exception: SyntaxError: Unexpected token []
.

If you have any suggestions on how to resolve this issue, please share your insights.

Answer №1

The issue arises from not properly closing the [] and the {} in your JSON structure

Your JSON structure should be formatted like so

{
   "items":[
      {
         "item-id":"id",
         "item-type":"type",
         "name":"name",
         "description":"desc",
         "price":"price",
         "quantity":"qty"
      }]
}

Answer №2

data = {
    "items": [{
            "id-number": "123",
            "id-type": "type1",
            "name": "Name1",
            "link": "link1",
            "description": "desc1",
            "date-added": "date1",
            "category": "cat1",
            "tags": "tag1,tag2"
        }
    ]
};

for (item in data['items'][0]) {
    console.log(data['items'][0][item]);
}

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

Could you please provide the NodeJS ES6 alternative syntax for importing the PouchDB library using the `let` keyword?

Currently, I am following a tutorial at https://medium.com/beginners-guide-to-mobile-web-development/getting-started-with-pouchdb-f0f3d7baebab on incorporating PouchDB into a server-side NodeJS script. However, I have encountered an issue in Step 1: cons ...

Information is only displayed within the Node Request function and is not accessible to

I am currently developing a small web scraping tool using Node (Express) to search URLs from a list. However, I'm encountering an issue with accessing the results of the search outside of the request callback function in a forEach loop. Can anyone hel ...

Ways to retrieve certain details from a Json array

After logging this array, I'm trying to extract the district data highlighted in yellow. Can anyone provide guidance on how to go about this? Please click here to view the image link of the array data in the log. ...

Cancel a batch upload request using AJAX

Currently, I am working on implementing a feature for aborting a multiple file upload process while also displaying the progress of the upload with a progress bar. My objective is to ensure that when the user clicks on the abort button, not only does the ...

Sharing the same instance of a class across various entry points in webpack output: A guide

Are separate instances of a class created when it is imported into different entry-point files of webpack? I need to import a class called AJAX and maintain the same instance throughout the project across all entry-point files. Currently, AJAX is used as ...

The style property is returned as null by the React Hook useRef

I am currently working on a modal that requires me to access the id property of an HTML element in order to modify its display property. I attempted to achieve this using the useRef Hook, but encountered the following errors. TypeError: Cannot read propert ...

How to search for multiple items with Node.js and MongoDB using Mongoose

Currently, I am looking to retrieve multiple items from my database without having to loop through each one individually. I have been advised against this approach and am seeking a more efficient solution. { "_id" : ObjectId("59d2b1cf8cec1709f85eb7a9"), " ...

Utilizing ASP.net's (VB code behind) functionality to efficiently parse JSON data retrieved from Facebook's

Seeking to experiment with ASP.net rather than my usual classic ASP, I am looking for examples on parsing out the name and id from a Facebook API Graph call JSON response. The JSON data retrieved from Facebook appears as follows: { "data": [ { ...

Populate Android RecyclerView with Images and Text from JSON Data

Hello, I am currently working on creating a RecyclerView list from JSON data that includes image URLs and names. I have spent the past two days searching Google and Stack Overflow for a solution, but I have not been able to figure out how to set the text a ...

Instructions for showcasing an image taken using cordova-plugin-media-capture

I recently implemented this specific plugin in my project which allows me to easily capture images using my Android device. However, I'm encountering some difficulties when trying to display the images that are captured within my app. After attemptin ...

unable to use split method in node js during put operation

I am currently working on an update action within my Node.js project. When new data is inputted, I am experiencing a peculiar issue. When I input fresh data, the split method functions properly. However, if I input the same data again, the split method fa ...

Creating a hyperlink automatically from text with Angular 4

Summary How do I display a clickable link in the browser from text received in json format? Situation I am developing in Angular 4 and retrieving data from an object inside a json file for a simple blog. The issue is that in the json file I am referencin ...

How can you merge arrays using values stored in an array of objects?

Having an array filled with various objects: let example = [ { name: 'object1', values: ["value1", "value2"] }, { name: 'object2', values: ["value3", "value4"] }, { name: 'object3', values: [" ...

Tips for designing a personalized vertical YAxis label in Recharts that will dynamically adjust to accommodate lengthy text

I'm currently working on creating a few composite charts using the Recharts library, and I've encountered an issue where some of the vertical labels on the YAxis are too long and getting cut off. Here's a picture showing the cut-off labels ...

Exploring the money library in typescript after successfully installing it on my local machine

I've been struggling to set up a new library in my TypeScript project for the first time, and I can't seem to get it functioning properly. The library in question is money. I have downloaded it and placed it in the root of my project as instructe ...

"Implementing drag and drop functionality in Vue.js without the need for a

Exploring html 5 drag and drop functionality using vue js has been a challenging experience for me. After following the w3schools tutorial on drag and drop, I managed to get it working in a basic html file but faced difficulties when implementing it in my ...

Unable to get HTML text input validation with RegEx to function, despite incorporating the required attribute

I am attempting to create dynamically generated text inputs that only allow for digits and an optional decimal point. I have added the required attribute but the inputs are not responding to the RegEx pattern. var howMuch = $("<input>").attr("type", ...

The child element is absolutely positioned within the parent element with a higher z-index

It is crucial for the parent absolute to be under child absolute. How can I resolve this issue using CSS? The positions of both elements must remain absolute. <div class="parent"> <div class="child">child</div> </div> ...

Tips for fetching paginated HTTP requests from a backend API using Observables within Angular 4

My Angular 4 app has a service that retrieves a variable number of pages from a back-end API. I came across a solution on Stack Overflow which suggests using flatMap, but it doesn't work for me as the number and URLs of requests are dynamic and unkno ...

Weird behavior observed with NativeScript Firebase and ES6 promises

My goal is to connect 2 Firebase queries using promises, as I require the result of the first query in order to execute the second one. Below are the details of the 2 queries: QUERY FOR VALUE A: private getValueA(){ var queryEvent = (result):void ...