Exploring the depths of a multi-level JSON array in JavaScript

My asp.net web service has the capability to return a multi-level array.

To parse the json string, I am using the json2.js library :

var data = JSON.parse(msg.d);

The first level of parsing is successful, but the second level array (data) remains as an array of objects

? data[0]

{...}
color: "#0000CD"
data: [[object Object],[object Object]]
label: "formol"
type: "traitement"

? data[0].data

[[object Object],[object Object]]
[0]: {...}
[1]: {...}

? data[0].data[0]

{...}
_l: ""
_x: 7
_y: 25

However, what I actually need is an array of data like this:

? data[0]

{...}
label: "traitement formol 2"
type: "traitement"
color: "#0000CD"
data: [7,25,,7,40,formol]

? data[0].data

[7,25,,7,40,formol]
[0]: [7,25,]
[1]: [7,40,formol]

? data[0].data[0]

[7,25,]
[0]: 7
[1]: 25
[2]: ""

Can anyone suggest the best approach to decode/parse all levels of the json string at once?

Thank you.

Answer №1

So far, my quest for a straightforward solution to convert the json string into a pure array has been unsuccessful. Currently, I am resorting to parsing the string and directly replacing the object with an array.

var data = JSON.parse(msg.d);

                for (var key in data) {

                    if (data.hasOwnProperty(key)) {

                        dataset[key] = data[key];

                        for (var subkey in data[key]) {

                            if (data[key].hasOwnProperty(subkey)) {

                                var level2 = data[key][subkey];

                                if (typeof level2 == "object") {
                                    for (var subsubkey in data[key][subkey]) {
                                        var level3 = data[key][subkey][subsubkey];
                                        dataset[key].data[subsubkey] = new Array(level3.x, level3.y, level3.l);
                                    }
                                }
                            }
                        }
                    }
                }

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

Modifying attributes of an object within a document using Mongoose

Encountering an issue where the sentiment object in my document is not updating. Within my Model Class, there's a field named sentiment of type Object structured like this: sentiment: { terrible: 0, bad: 0, okay: 0, good: 0, fantastic: 0 } ...

What is the best way to obtain a reference to the parent form element within an AngularJS directive?

Here is the HTML code I am working with: <form name="my-form"> <div class="col-sm-4"> <input type="phone" name="mobileNumber" ng-model="form.mobileNumber" value="0439888999" required> </div> </form> In addition, I ha ...

The link function of an Angular directive is failing to execute

After following the ng-bbok tutorial, I encountered a strange issue where the code inside the link function was not being executed. It turned out that the problem stemmed from having an empty compile function before the link function in the directive defin ...

Access information within the collection view

After downloading data from a JSON File, it is now stored in an object specific to that data. The goal is to access this data in a collectionView upon clicking on it. The question arises whether the data needs to be made globally available for all function ...

extracting information from an object's property

I have implemented the following JavaScript code: var hours = $(".input_hr"); var minutes = $(".input_min"); var categories = $(".input_cat"); for(var i=0;i<categories.length;i++){ if (categories[i].value === "Entertainment") { ...

Utilizing visual representations for "symbol" within eCharts4r

I have been exploring the use of the "image" option for the symbol parameter in a tree chart with eCharts4r. Despite trying multiple methods, I am struggling to assign a unique image to each node in the tree instead of using a universal one. However, my a ...

Is it possible to access the names of objects within an array in JavaScript?

If objects are created and placed in an array, does the array store only the properties of the objects or also their names? This may seem like a simple question, but I've been unable to find a clear answer. var boxA = {color: "red", width: 100}; var ...

Examining - Assessing the Link (next/link)

I am currently working on writing unit tests to verify the functionality of my navigation links. Here is a snippet from my MainNavigation.js file: import Link from 'next/link'; const MainNavigation = () => { return ( <header> ...

Achieving a balanced css navbar size that fits the page dimensions

I'm encountering an issue with my navbar where it gets distorted when the window size is too small. How can I make it shrink proportionally to fit the page? I've attempted several solutions, but they only reduce the text size without properly res ...

Arranging several arrays at the same time

Looking for a solution to efficiently sort multiple arrays based on one array while keeping values at the same index position in sync. Is there a method to achieve this? Example: Consider three arrays: String[] distance = [1,3,6,7,9]; String[] name = [J ...

Maintain focus on a particular section of the webpage after submitting the form using Javascript

I am looking to achieve a specific functionality where the form is submitted when the user presses a button, and upon page reload, the users should remain on the same page. I have a Worker/Buyer form setup. In cases where the user is a buyer and fills out ...

The Jenkins build on a specific branch is encountering issues with path exporting, causing it to fail

I have a set of files related to a new feature that I am trying to deploy through Jenkins. I have successfully deployed other branches in the past, but I am encountering difficulties with a specific branch. https://i.sstatic.net/BKVbH.png I believe the pro ...

Retrieve the initial entry from a json file with PHP before proceeding

My JSON file contains data structured as follows: [{ "name" : "Mak", "registration_code" : "Registration code", "date" :"date", "time" : "time", "file_data" :{ "Feature_0" : "0,0.956222737454317," } }, { "name" : " ...

The font in my Next.js / Tailwind CSS project starts off bold, but unexpectedly switches back to its original style

I recently integrated the Raleway font into my minimalist Next.js application with Tailwind CSS. I downloaded the font family in .ttf format and converted it to .woff2, but I'm having trouble changing the font weight using custom classes like font-bol ...

Fusion: Combination of Drop-down Menu, Inactive Text Field, and Database Data Retrieval

I am currently working on a form that allows users to either select a new team and enter its location, or choose a team from a list and have the location automatically filled in the input box. However, my current code is not functioning as expected. <s ...

Can someone explain what exactly is [object Object]?

Why is the data value from the database showing as [object Object] instead of the actual data? var dataObj = $(this).closest('form').serialize(); $.ajax({ type: "POST", url: url, data: dataObj, cache: false, ...

In Next.js, the elements inside the div created by glider-js are not properly loaded

I'm currently working on setting up a carousel in nextjs using the data retrieved from an API and utilizing glider-js for this purpose. However, I'm facing an issue where the div created by glinder-js does not include the elements that are render ...

Displaying a webpage within a div section of another webpage by referencing it from a separate webpage

If I have three html pages named index.html, page1.html, and page2.html. Imagine that I am currently on page2.html and there is a list of items, each linking to different pages. Let's say one item links to page1.html. Is it feasible to load page1.ht ...

Error message in MongoDB Compass: JSON input terminated unexpectedly

I recently exported my MongoDB Collections locally as JSON files on my personal computer. Now, I am attempting to import these Collections onto my root server using MongoDB Compass. However, every time I try to export the Collection, I encounter the follow ...

Leveraging URL parameters within node.js and Express

Having no prior experience with node, I decided to delve into the Express project in VS2019. My goal was to create master/detail pages with a MongoDB data source. In my pursuit, I configured three routes in /routes/index.js. //The following route works as ...