The Javascript JSON object is reporting an error, but the JSON validator is indicating that the

Upon receiving a Json object from an API, I realized that it contains numerous backslashes and despite passing it through a json validator, it is marked as valid. When using this json object in JavaScript, everything seems to work fine until the final section where I encounter an error. Surprisingly, if someone else follows the same steps with my json string, they might not face any errors:

let ResponseJson = {"config":{"data":"market-pairs","limit":1,"symbol":"'LTC'","page":0},"usage":{"day":112,"month":262},"data":[{"name":"Litecoin","symbol":"LTC","total_rows":2221,"marketPairs":[{"id":83223,"asset_id":4,"exchange_id":113,"unique_key":"4:citex:LTC:USDT","1d":"{\"volume\":\"196939102.19\",\"volume_base\":\"1119444.67\",\"volume_change\":\"-29686113.31\",\"volume_base_change\":\"-142615.38\",\"price_change\":\"4.71070407\",\"price_quote_change\":\"...
 
let size=Object.size(ResponseJson.data[0].marketPairs)-1;
console.log(size);
alert(typeof(ResponseJson.data[0].marketPairs[0]));

The type of object is correctly identified and its size can be determined without any issue. However, when attempting to extract a specific value from 'marketPairs', an error surfaces:

console.log(ResponseJson.data[0].marketPairs[i].1d_volume);

This results in the following error message:

Uncaught SyntaxError: missing ) after argument list

I am currently unaware of a solution to resolve this issue. Does anyone have any suggestions?

Object.size = function(obj) {
  var size = 0,
    key;
  for (key in obj) {
    if (obj.hasOwnProperty(key)) size++;
  }
  return size;
};

let ResponseJson = {"config":{"data":"market-pairs","limit":1,"symbol":"'LTC'","page":0},"usage":{"day":112,"month":262},"data":[{"name":"Litecoin","symbol":"LTC","total_rows":2221,"marketPairs":[{"id":83223,"asset_id":4,"exchange_id":113,"unique_key":"4:citex:LTC:USDT","1d":"{\"volume\":\"196939102.19\",\"volu...
 
let size=Object.size(ResponseJson.data[0].marketPairs)-1;
console.log(size);
alert(typeof(ResponseJson.data[0].marketPairs[0]));
console.log(ResponseJson.data[0].marketPairs[i].1d_volume);

If you view the code snippet above, you will notice that the error persists within it as well.

Answer №1

If you encounter an error message after adding a specific line, then the issue likely lies with that line. In JavaScript, variable names cannot start with a number, but you can still access the object like a map instead of a member variable. This can be achieved by using bracket notation instead of dot notation. Check out the syntax here:

console.log(ResponseJson.data[0].marketPairs[i]["1d_volume"]);

Implementing this change should resolve your problem.

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

Is there a solution to the Chrome issue "Require user interaction for beforeunload dialogs" that arises while running Cypress tests?

Require user gesture for beforeunload dialogs A new feature has been implemented where the beforeunload dialog will only be displayed if the frame attempting to show it has received a user gesture or interaction, or if any embedded frame has received su ...

AngularJS $routeProvider not working as expected

Today marks the beginning of my journey with AngularJS, and I'm facing a roadblock in the "routing" aspect. I've successfully created a controller and a view (refer below), but upon attempting to run it on my local server, an error pops up: Unca ...

Encountering issues with basic login functionality using Node.js and MongoDB - receiving a "Cannot

I'm experiencing some difficulties trying to set up a login for my website. I have a user registered in my database with the email: [email protected] and password 123. The issue arises when I attempt to use the POST method, as it returns the fo ...

Converting JSON data into a hierarchical model structure

My code includes the following model: public class CustomerInformationInputModel { public CustomerInformationInputModel() { PrimaryCustomerInformation = new PrimaryCustomerInformationInputModel(); ...

Building a div element within a React function

For an exercise, I need to create an input field and a button. The goal is to display the text from the input field in a div/span below when the button is clicked. If I change the text in the input field and click the button again, the displayed text shoul ...

Getting the final element in a MySQL JSON array: Tips and tricks

After finally upgrading to MySQL 5.7, I've been exploring the new JSON functions and they are quite impressive! I encountered a scenario where I needed to retrieve the last element in a JSON array. It's simple when you know the ordinal position ...

Searching for objects with distinct hash codes using an AngularJS filter

My title may be long, but I want to explain something: I am using a service called: divisionService This service has a function that returns a list of all Divisions in my system. I use this list to populate a select: <select class="form-control ...

Obtaining information from connected brushes in mlpd3, Bokeh, and Plotly using Python

In the following code, a 2x2 graph with 4 plots is generated. Data points can be selected using brushes. The main question here is how to retrieve the selected data points as a JSON array or CSV file. While this code uses mpld3 for this purpose, Bokeh offe ...

A JSON string encoded using urlencode is passed in the URL

My current framework is CodeIgniter 3.x. A user has provided the following data: %7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D The user sends a request and includes this data as a parameter in the URL. The data at the ...

Is it possible to use @ViewChild to target an element based on its class name?

The author of this article on Creating Advanced Components demonstrates selecting an element by creating a directive first: @Directive({ selector: '.tooltip-container' }) export class TooltipContainerDirective {} Then, the author uses this d ...

Is there a way for me to acquire more information on event bubbling in web development, specifically relating to the YUI framework?

Are there any recommended resources for learning about event bubbling, specifically within the context of the Yahoo User Interface libraries (YUI)? ...

Using setInterval() does not automatically fetch JSON data at a specified interval for me

Attempting to constantly update the latitude and longitude coordinates from a dynamic API has proven to be a challenge. Despite utilizing jQuery with a set interval of 1000 milliseconds, the data retrieval from the API does not occur as expected. While i ...

Having issues with ajax posting functionality

Is the following form correct? <form> <input type="submit" value="X" id="deleteButton" onclick="ConfirmChoice(<?php echo $id; ?>)" /> </form> Here is the function associated with the form: <script type='text/javasc ...

Retrieve all records in MySQL that have the same value in this particular field

Explaining my MySQL issue in words is tough for me as a beginner, and finding solutions is proving really challenging. This problem is much clearer when seen visually: I am trying to select exchange_pair_id's that have the same pair_id to form an ar ...

Enhancing Serialization in Scala Case Classes with Computed Fields

My question pertains to a case class I have in Scala: case class Person(name: String, age: Int) { def isMillenial: Boolean = age <= someConstantMillenialThreshold } Initially, when this class is serialized using the ScalaJsonFactory object mapper, ...

When using Google login in React and Node.js, the requested scope may not be returned as expected

Looking to get additional scope data from Google API during login on my app. I am utilizing react-google-login to obtain a token in my React application with the following scopes: scope='https://www.googleapis.com/auth/user.birthday.read https://www. ...

Retrieve the date one week prior to today's date in Node.js and format it in Mysql style

I need to find the exact date from one week ago in SQL format using Node.js. I attempted a similar solution as described here - How to get yesterday date in node.js backend? but unfortunately it's not working for my specific case. ...

What is the reason the useEffect hook does not function properly with a state variable within context?

Check out my code here. I'm trying to display the content of the array testingData, but it's not showing up. If I remove the useEffect hook, it works fine. Can you help me understand why and how to fix it? ...

Is there a way to modify the style value within AngularJS?

<div class="meter"> <span style="width: 13%"></span> </div> I have the following HTML code snippet. I am looking to update the width value using AngularJS. Does anyone have a solution for this issue? ...

JavaScript still mentions a class that no longer exists

One of my elements has a class of .collapsed. When this element is clicked, a jQuery function is triggered to remove the .collapsed class and add the .expanded class instead. Even after the .collapsed class is removed, the function I have created continue ...