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
Disabling a Button Within a Stateless Component - React
How to Get Route Url Params in a Page in Nuxt2 and 3
Warning: Failed Proptype: Invalid Prop 'Component' Supplied to 'Route'
Using Setstate to Change Multiple Values Within an Array of Objects - Reactjs
How to Set an Iframe Src Attribute from a Variable in Angularjs
How to Capture the Browser Window Close Event
Reinitialize/Reset Dropzone After Form Is Submitted
How to Check If Element Has Focused Child Using JavaScript
How to Listen to the Window Scroll Event in a Vuejs Component
How to Prevent Browser to Re-Set Inputs on Page Refresh
Merge Two Json/Javascript Arrays in to One Array
How to Change Background Color of a Row on Checkbox Selection
No Overload Matches This Call. Type 'String' Is Not Assignable to Type 'Signals'
Change Image on Scroll Position
Call Ajax or Submit the Button When Checkbox Is Checked
How to Get the Value of Li on Dropdown Menu Click in Laravel Using Jquery
Getting Typeerror: This.Props Is Undefined on Reactjs
Extracting Key:Value Pairs Assoc With Regex from String on JavaScript