I want a JSON array to contain all integers between 0 and 1000, but I don’t want to declare each element of that very long array individually. Is there a more economical/elegant way to declare that array in a JSON file?
Question posted in Json
Our archive of expertly curated questions and answers provides insights and solutions to common problems related to this popular data interchange format. From parsing and manipulating JSON data to integrating it with various programming languages and web services, our archive has got you covered. Start exploring today and take your JSON skills to the next level
Our archive of expertly curated questions and answers provides insights and solutions to common problems related to this popular data interchange format. From parsing and manipulating JSON data to integrating it with various programming languages and web services, our archive has got you covered. Start exploring today and take your JSON skills to the next level
2
Answers
No, all elements in JSON must be listed explicitly. Of course if you are producing this JSON document from some script/program there will surely be terser ways to generate it, but the resulting JSON will list all the elements without shortcuts.
No, you can’t. However, JSON is just a convenient way to serialize and de-serialize data and has a mechanism to restore data, called
reviver
.For the task you want, assuming you want the property
numbers
to be an array of 0 to 1000, I’d do this:And, when you parse it, you can use the
reviver
function: