What is the method for determining the size of nested JSON data structures?

When I load a JSON into a variable, I encounter an issue in determining the length of the nested JSON.

var JVarible = [{"key":{"kind":"Comment","id":5992578889547776},"categoryId":0,"userName":"Shana Deepak","userId":"cpshana","comment":"hi.fghfghfgh ","createDate":"Sep 16, 2013 7:07:36 AM","url":"https://graph.facebook.com/100000840303512/picture?type\u003dsmall","networkType":"facebook","status":1,"nestmsgs":{"value":"[{\"key\":{\"kind\":\"Nestmsg\",\"id\":5914238686068736},\"commentId":5992578889547776,\"userName\":\"Shana Deepak\",\"userId\":\"cpshana\",\"message\":\"dfgdfgfdg\",\"createDate\":\"Sep 16, 2013 7:22:01 AM\",\"url\":\"https://graph.facebook.com/100000840303512/picture?type\\u003dsmall\",\"networkType\":\"facebook\",\"status\":0},{\"key\":{\"kind\":\"Nestmsg\",\"id\":5281469744283648},\"commentId":5992578889547776,\"userName\":\"Shana Deepak\",\"userId\":\"cpshana\",\"message\":\"gfdgdfgfd\",\"createDate\":\"Sep 16, 2013 7:12:25 AM\",\"url\":\"https://graph.facebook.com/100000840303512/picture?type\\u003dsmall\",\"networkType\":\"facebook\",\"status\":0}]"}}];


var i=0;

 for (i=0; i<JVarible.length;i++)
 {

 alert(JVarible[i].['nestmsgs'].length)

}

Answer №1

Let's clarify a few things here - the variable JVarible actually holds a JavaScript array, not JSON data. However, inside this array, there is a specific value that is in fact JSON data (stored as a string).

It seems like you may be looking to count the number of messages within each object. The object nestmsgs contains a single property called value, which holds a JSON string.

To achieve your goal, you need to first parse the JSON string into an array and then find its length using the following code snippet:

for (var j = 0; j < JVarible.length; j++) {
    var messageObject = JVarible[j].nestmsgs;
    messageObject.value = JSON.parse(messageObject.value);
    alert(messageObject.value.length);
}

Answer №2

Do you mean something along these lines?

Object.keys(dataSample[0]) //outputs ["id", "category", "name", "user_id", "content", "created_at", "link", "network", "active", "messages"]

Object.keys(dataSample[0]).length //equals 10

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

Text transitions in a gentle fade effect, appearing and disappearing with each change

I want to create a smooth fade in and out effect for the text within a div when it changes or hides. After researching on Google and Stack Overflow, I found that most solutions involve adding a 'hide' CSS class and toggling it with a custom func ...

Collaborating with SockJS connectivity

Currently, my Node.js backend is interacting with desktop clients using websockets. The communication from the server side is initiated from a web front-end and everything is functioning properly because I am storing the SockJS Connection instances in an ...

Decipher the JSON data stored in an object

I have been attempting to parse a JSON string within a larger JSON object but keep encountering errors such as "Unexpected Token" or "unexpected end of input." Any assistance on this matter would be highly appreciated! The structure I am working with is a ...

Utilizing JavaScript to dynamically alter an image based on selected dropdown option

I am currently facing an issue with my code where the selected image is not changing when I choose an option from the dropdown list. There are a total of 5 images, but for some reason, they are not displaying correctly. Here is the snippet of my code; < ...

What is the best way to calculate the time difference between startdatetime and enddatetime using JavaScript?

I need help calculating the difference between startdatetime and enddatetime in both days and time. Here is the html code I'm using: StartDate:<input size="16" type="text" placeholder="YYYY-MM-DD" id="from_date" name="from_date" value=""> End ...

`Understanding JSON Parsing in Java`

Can someone help me with parsing the JSON text to extract "id": 176514? I'm struggling to find the correct code. { "response": { "count": 10307, "items": [ { "id": 176514, "from_id": - ...

Leverage the power of index to slice through an array in Apps Script

Having recently transitioned from Matlab to apps script, I am facing a challenge with subsetting arrays. Specifically, I need to filter the array "names" based on the values in another array called "index", which are of equal length: names = [["name1"], [ ...

Having trouble with non-functional binary code in jQuery AJAX

Having trouble with my image upload function code. The variable is not getting posted via ajax, any assistance would be appreciated. <script type="text/JavaScript"> $(document).ready(function() { $("#btn").click(function() { $.get("i.png ...

There seems to be an issue with the calculation in my React app as it only displays the correct result after the second symbol

I've been developing a react app that features a button labeled "Add" for users to input data to calculate volume. Each line includes two input fields for the thickness and surface area values. I've noticed an issue where, if a user inputs the th ...

Customizing the initial search parameters in Vue InstantSearch: A step-by-step guide

I am utilizing the Vue components from Algolia to perform a search on my index. The search functionality is working correctly, but I am curious about setting the initial values for the refinement list upon page load. This is how I have set up the search: ...

SCRIPT5007: The property 'href' cannot be assigned a value as the object is either null or undefined

This script is functioning properly in browsers like Chrome and Firefox, however it is encountering issues when used with Internet Explorer. An error message is displayed in the IE console: SCRIPT5007: Unable to set value of the property 'href': ...

How can you utilize parameters from a JSON file to query a database within a PHP environment?

I've been grappling with using JSON in my iPhone app to fetch data from my SQL database via PHP. Sending and retrieving the JSON works fine, but I hit a roadblock when it comes to extracting parameters from the sent JSON array. Something seems off wit ...

What is the best way to determine the reason for a npm dependency being installed?

Looks like this question may have been asked before, but I haven't been able to locate the answer in my search. I recall that NPM offers a command such as npm why module-name Or npm explain module-name This command helps you understand why a par ...

Creating duplicates of a parent mesh in THREE.js with its children and additional materials

I am inquiring about a cloning issue I am having with a mesh that has a parent and 4 children, each with different materials. When I clone the parent mesh using this code: let result = cloudObjects.sideCloudGeometry[texture].clone(); The cloned mesh look ...

What steps should I take to handle an exception when the maxJsonLength is exceeded in JavaScriptSerializer?

I have encountered a unique issue in my context of a large request, where existing solutions for handling large responses do not apply. When attempting to post a large file to an action method, I receive the following exception: Error during serializati ...

New and one-of-a-kind C# 90-ball Bingo cards

I am developing a bingo card generation system that includes 6 tables. Each table contains 3 rows and 9 columns, with 5 numbers and 4 empty spots in each row. The columns are divided as follows: Column 1: 1-9, Column 2: 10-19, Column 3: 20-29, Column 4: 3 ...

Can you explain the purpose of `import type {Node} from 'react';` and how it is used in the App component as: () => Node?

Executing the following command: npx react-native init AwesomeProject When reviewing the App.js file, I came across two lines that confuse me: import React from 'react'; import type {Node} from 'react'; // Line 1 import { SafeAreaVi ...

There seems to be a glitch in the functionality of annotations when using annotator.js

Currently, I am utilizing annotator.js to store the range in mysql. The following code fragment is being used for highlighting text within my file: <script src="/js/pdfjs/annotator.js"></script> <script> $(function(){ var annotation ...

Interfacing with Web Services using C# GUI

Currently, I am running a web service on a local server which includes a method that generates a JSON representation of an Oracle database query converted from a List<Dictionary<String, String>> object. However, my GUI program also requires th ...

How can we display the data returned by a successful AJAX request

Having an issue with displaying Ajax success data. success: function(data){ alert(need to print it here); } When I try to access the data using: console.log(data.responseText); {"success":false,"errors":{"text":["Some text.","some more text"]}} Any ...