Get Size of Json Object

How to find the length of a JSON object

Iterate over the array and get object property names count.

var legend = [{  "min": 0,  "max": 'first_color',  "color": "#1a9850"}, {  "min": 'first_color',  "max": 'sec_color',  "color": "#fee08b"}, {  "min": 'sec_color',  "max": 'thrd_color',  "color": "#ff3300"}, {  "min": 'thrd_color',  "max": 'frth_color',  "color": "#d73027",  "Abc": "gsfg"}];
var res = legend.map(function(v) { console.log(Object.keys(v).length); return Object.keys(v).length;});
console.log(res);

How to get the length of the json object?

If you look at the output in the console you'll see that data is an array.

To get the length of an array you can simply use.

var length = data.length

In case data would be an object and you want to see how many keys are present. Then you'll use.

var length = Object.keys(data).length;

In your example:

If you use data.length, it will return 5 as your array has the length of 5.

If you use Object.keys(data[0]).length, it will return 2 as you have two items (name and location) in the first array element.

More information on arrays and objects can be found below.

MND - Array's

MDN - Object.keys

How to get a length of json object saved in local project directory?

I would use the jackson-databind library, using the class JsonNode.

The latest version on the maven repository is 2.11.1

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.1'

Here is an example for getting the size with jackson-databind:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

public class GradleApplication {
public static void main(String[] args) {


try {
File jsonFile = Paths.get("src", "main", "resources", "example.json").toFile();
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonFile);
System.out.println("The size of the json is : "+ node.size());
} catch (IOException e) {
e.printStackTrace();
}


}
}

How to find the length of the JSON in flutter

You can get the length of json like this:

stores = json.decode(response.body);
final length = stores.length;

And get the list of items to use in a drop down widget:

  List<String> items = [];
stores.forEach((s)=> items.add(s["store"]));

new DropdownButton<String>(
items: items.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (_) {},
)


Related Topics



Leave a reply



Submit