How to get the length of object within a JSON? - json

How do I get the length of an object stored within a JSON?

Object.keys can be used to return an array of all the keys of the object and then you get the length of that array - that's the number that you are looking for.
console.log( Object.keys({"0":89,"1":54,"2":34,"3":67,"4":131}).length );

Related

How can I use a json file if all elements are in one array?

I'm trying to learn to import JSON files into p5.js.
I'm using this list of English words: https://raw.githubusercontent.com/dwyl/english-words/master/words_dictionary.json
I already know how to import things when the entries are organized into respective categories. It seems every entry is in the same array, though. How can I import individual entries?
In this particular case the data you're loading isn't a regular array ([]) with values accessed using an integer index, but a JavaScript Object or associative array({}) with values accessed using a string instead of an integer index.
This is a key-value pair association.
Let's say you have a simple association:
let data = {
"firstName": "Nicholas",
"lastName": "Keough",
}
In the example above firstName and lastName are keys and "Nicholas" and "Keough" are values.
You can use array like [] and instead of an integer index, you'd use the key:
console.log(data["firstName"]);// prints "Nicholas"
console.log(data["lastName"]);// prints "Keough"
alternatively you can use dot notation:
console.log(data.firstName);// prints "Nicholas"
console.log(data.lastName);// prints "Keough"
In your case, let's say dictionary is a variable that holds the loaded JSON data,
you apply the same ideas above to access each word. For example:
console.log(data['absorbent']);//prints 1
or
console.log(data.absorbent.);//prints 1
If you need to loop over the values (and in your case, with the dictionary of 370101 values, you would really want to), you can use a for...in loop.
Alternatively, if you simply need to access the keys, not the values, as a single array you can use Object.keys()
Here's a basic example illustrating both options:
(Bare in mind the dictionary is large so it will take time to load and print the data to console)
let dictionary;
// preload JSON data
function preload(){
dictionary = loadJSON("https://raw.githubusercontent.com/dwyl/english-words/master/words_dictionary.json");
}
function setup(){
noLoop();
console.log('test');
// access JSON data with a for...in loop
for(let word in dictionary){
console.log("word: " + word + " value: " + dictionary[word]);
}
// optionally, access the associative array keys only as an array
console.log(Object.keys(dictionary));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script>

How to get value from this json object

Here the json data. [1,"GarageC","NUR841",1611241200]
how do i get the GarageC out from json.decode?
This is array you can retrieve value from array by index basis like array_name[index]

How to extract value from a nested json array?

i have a json object which contains json data with a key. now i want to extract value from that json object like name, address etc and store them to variables.
controller
json_arr = new JSONArray(j_str);
int count = json_arr.length();
json_o.put("user", json_arr);
j_str contains following data
[{"Bollywood":[{"actor":[{"name":"AA","gender":"Male"},{"name":"BB","gender":"Male"}]}]},{"Hollywood":[{"actor":[{"name":"CC","gender":"Male"},{"name":"DD","gender":"Male"}]}]}]
now it is converted to json object -- json_o ,, putting a key --- "user". now how can get a specific data such as 2nd actor name from hollywood. (i.e value DD). after then store that to a string.
Short answer: Use Jackson to map the json string to a java object, and then extract that value as a variable.
Here is a quick guide on doing this with jackson: http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

Get length/size of json data

I have use the following codes to get my json data with certain nodes.
console.log(Object.keys(data.Top["person"]).length);
It is work fine in following structure of json data array:
data:{"Top":{"person":[{"A":"a","B":"b"},{"A":"a","B":"b"}]}}
However when the person only have one nodes, it always show the answer 2 to me, it should be answer 1.
data:{"Top":{"person":{"A":"a","B":"b"}}}
Is it possible to solve this error?
length property is supported by type array.
data:{"Top":{"person":[{"A":"a","B":"b"},{"A":"a","B":"b"}]}} in case of this person is array enclosed with [ ]
Where as for data:{"Top":{"person":{"A":"a","B":"b"}}} person is just an object. Hence length is undefined for it.
If you are creating json out of string or formatting it make sure to include [ and ] for person attribute.
JSFiddle
https://jsfiddle.net/d6pqnckh/
Also use JSON formatter to test JSON structure.
UPDATE
Since you are not sure of JSON structure. What you could do is before accessing length of person check if it is an array or object.
if(Object.prototype.toString.call(data.Top.person) === '[object Array]')
alert(data.Top.person.length);
//It is an array
else
alert("I AM OBJECT"); //It is of an object type
Updated Fiddle: https://jsfiddle.net/wys7awuz/
To make it an array regardless https://jsfiddle.net/ofkboh6k/
var p = data.Top.person;
delete data.Top.person;
data.Top.person = [];
data.Top.person.push(p);
alert(data.Top.person.length);
Include this in else part of condition. It will make it an array.
length works for type array.
change your JSON to
data:{"Top":{"person":[{"A":"a","B":"b"}]}}

JSON.parse() returns alphabetical order

I'm parsing my string value to json object.
while doing this it returns the json object with different order.
var l = '{"creationType":"ITEMCLASSES","maxLevelNo":"4","selectbox1":"11001","textbox11":"KRAFT PAPER","hidden11":"11001","textbox12":"FG2","hidden12":"11051","textbox13":"HMC2","hidden13":"11045","textbox14":"2","textbox15":"LS2","hidden15":"11048","textbox16":"123","hidden16":"11015","selectbox2":"11002","textbox21":"kraft ","hidden21":"11057","selectbox3":"11003","textbox31":"40 BF","hidden31":"11004","textbox32":"Natural Color","hidden32":"11006","textbox33":"2","textbox41":"140 GSM","hidden41":"11008"}';
var o = JSON.parse(l);
alert(0);
resultant object is ordered based on the name.
how to avoid this situation?
kindly give your suggestion.
Your variable "1" is already a JSON object. This means that feeding it into the JSON.parse function is both redundant and unnescesarry. The JSON.parse(); function is primarily used when converting strings to JSON objects.
var l = {"creationType":"ITEMCLASSES","maxLevelNo":"4","selectbox1":"11001","textbox11":"KRAFT PAPER","hidden11":"11001","textbox12":"FG2","hidden12":"11051","textbox13":"HMC2","hidden13":"11045","textbox14":"2","textbox15":"LS2","hidden15":"11048","textbox16":"123","hidden16":"11015","selectbox2":"11002","textbox21":"kraft ","hidden21":"11057","selectbox3":"11003","textbox31":"40 BF","hidden31":"11004","textbox32":"Natural Color","hidden32":"11006","textbox33":"2","textbox41":"140 GSM","hidden41":"11008"};
alert(1);
Just skip the second line and manipulate your "1" variable directly.
You can't.
As described by www.json.org
In JSON, they take on these forms:
An object is an unordered set of name/value pairs
Use an array if you want to maintain the order.