How do I convert a multi dimensional JSON object into javascript array - javascript

I have a JSON object which looks like this:
[{"tabname":"orders","datagroups":[{"dataname":"ordersToday","datavalue":9},{"dataname":"orders30Days","datavalue":126}]}]
When I use console.log($.parseJSON(thedata))
I just get the word Object and no actual data.
How do I organise this data into a multidimensional javascript array? so that it looks something like this:
array("tabname"=>"orders", "datagroup"=>array(array("dataname"=>"ordersToday", "datavalue"=>9),array("dataname"=>"orders30Days","datavalue"=>126)))

It is an array:
var json = '[{"tabname":"orders","datagroups":[{"dataname":"ordersToday","datavalue":9},{"dataname":"orders30Days","datavalue":126}]}]';
var obj = $.parseJSON(json);
Array.isArray(obj) // => true

It's quite simple, really.
You can simply use jQuery's $.parseJSON (jsonString).

Thanks to everyone for contributing. I took a break then came back and figured it out. The way my brain works is all wrong.
To access the individual values, I needed to do something like this:
var orderStats = $.parseJSON(data);
console.log(orderStats[0].datagroups[0].dataname);

Related

Need to put this object into an array, how?

I need convert following in the EXACT format shown below with javascript, could you please suggest how to achieve this
from: {"healthy":true,"unhealthy_reasons":[]}
to: [{"healthy":true,"unhealthy_reasons":[]}]
If that's all you need to do, you can just wrap array brackets around the variable that contains the object:
let initialObject = {"healthy":true,"unhealthy_reasons":[]};
let arrayedObject = [initialObject];
But I'm wondering if there's more to this. If this is actually part of a more complicated task, just add that to your question and you'll get a more complete answer.
Use JSON.parse() and JSON.stringify()
let data = '{"healthy":true,"unhealthy_reasons":[]}';
let parsed = JSON.parse(data);
//TO get an array
console.log([parsed])
//TO get a string
console.log(JSON.stringify([parsed]))

How to search in nested JSON data in vuejs?

I have nested JSON data like:
[{"id":"1","province_id":"ABC","name":"City One"},{"id":"2","province_id":"EFG","name":"City Two"}]
I want to filter the JSON by province_id and put it in another variable. Is there any solutions in VueJS such as Vue.filter(); ?
I know we have "linq" which does the job but I do not want to use it.
what you're looking for is the javascript Array's filter() function. You should definitely spend some time getting familiar with filter, along with others like map and reduce. It'll make slicing and dicing your data much easier.
var serializedData = `[{"id":"1","province_id":"ABC","name":"City One"},{"id":"2","province_id":"EFG","name":"City Two"}]`;
var data = JSON.parse(serializedData);
var provinceAbc = data.filter(d => d.province_id === 'ABC');
That line will get you all objects where its province_id is "ABC"
Also, since you mentioned "linq" in your post, filter() is like IEnumerable.Where(), and map() is like IEnumerable.Select() in .NET Linq terms
I think this will work for you:
var nestedJson = `[{"id":"1","province_id":"ABC","name":"City One"},{"id":"2","province_id":"EFG","name":"City Two"}]`;
var array = JSON.parse(nestedJson);
array = array.map(e => e["province_id"]);
console.log(array);

Conversion of string to list from a file

I have a file in which the data(list) looks something like this
[5,[5,[5,100,-200],200,-400],300,-500]
Now when I read this file in an angular application, the file contents would be converted to string and hence would return a string object which would look something like this
"[5,[5,[5,100,-200],200,-400],300,-500]"
Is there a way where I can convert this back to the list in which it was originally present?
There is one approach but the solution is for a different problem. If my file has data
200
300
400
500
Then I can split this string by using
var newData = fileContent.split('\n');
desiredList = []
for(var z=0;z<newData.length;z++){
desiredList.push(parseInt(newData[z]))
}
The desired list would give me a list which I wanted. But for the question which I asked is there any approach?
JSON.parse("[5,[5,[5,100,-200],200,-400],300,-500]") should convert it to JavaScript object.
You can use eval()
var str = "[5,[5,[5,100,-200],200,-400],300,-500]";
var jsonArr = eval(str);
console.log(jsonArr);

Transforming this JSON via underscore or a javascript utility library?

I have some JSON that looks as so:
[{"target": "mydata.12.2", "datapoints": [[763.7, 1368821100], [762.1, 1368821160], [223.11, 1368821220], [886.54, 1368821280], [112.3, 1368821340]]}]
I'd like to remove the first key and surrounding array brackets so it reads:
{"datapoints": [[763.7, 1368821100], [762.1, 1368821160], [223.11, 1368821220], [886.54, 1368821280], [112.3, 1368821340]]}
My javascript parsing skills leave a bit to be desired and I was hoping someone could help. Perhaps underscore.js has something that could assist here?
No need for a library. Supposing your first array is called obj1, you just have to do
var obj2 = obj1[0];
delete obj2['target'];
Note that you seem to have no JSON here, just plain javascript objects.
Supposing you really want to start from JSON and get some JSON at end, parse then stringify :
var obj2 = JSON.parse(json1);
delete obj2['target'];
var json2 = JSON.stringify(obj2);

How to convert this string to some array?

I am using one 3rd party plugin which uses stringify and gives me something like:
["ProjectB","ProjectA","Paris"]
It was an array but it used stringify and serialized into this format.How do I get back my array from this? Now I could very well use split and then remove 1st and last character from every string and get it but I don't want to do that manually. Is that any built in utility that can do that for me?
Assuming you have like var str = '["ProjectB","ProjectA","Paris"]';
Try using,
var array = JSON.parse(str); //will return you an array
As #AlexMA pointed out: JSON.parse is not supported in old browsers so you are better off using jQuery version like below,
var array = $.parseJSON(str);
You can use
JSON.parse(your_arr_str);
or jquery
$.parseJSON(your_arr_str);

Categories

Resources