This isn't a single JSON object. You have an array of JSON objects. You need to loop over array first and then access each object. Maybe the following kickoff example is helpful:

var arrayOfObjects = [{
  "id": 28,
  "Title": "Sweden"
}, {
  "id": 56,
  "Title": "USA"
}, {
  "id": 89,
  "Title": "England"
}];

for (var i = 0; i < arrayOfObjects.length; i++) {
  var object = arrayOfObjects[i];
  for (var property in object) {
    alert('item ' + i + ': ' + property + '=' + object[property]);
  }
  // If property names are known beforehand, you can also just do e.g.
  // alert(object.id + ',' + object.Title);
}

If the array of JSON objects is actually passed in as a plain vanilla string, then you would indeed need eval() here.

var string = '[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]';
var arrayOfObjects = eval(string);
// ...

To learn more about JSON, check MDN web docs: Working with JSON .

Answer from BalusC on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_arrays.asp
JSON Arrays
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_objects.asp
JSON Literals
JSON cannot be an object. JSON is a string format. The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ javascript json array of objects
How to Use Array of JSON Objects in JavaScript | Delft Stack
February 2, 2024 - We can create an array of JSON object either by assigning a JSON array to a variable or by dynamically adding values in an object array using the .push() operator or add an object at an index of the array using looping constructs like the for loop or while loop.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Learn_web_development โ€บ Core โ€บ Scripting โ€บ JSON
Working with JSON - Learn web development | MDN
The objects and arrays inside JSON need to further contain valid JSON data types. Strings must be enclosed in double quotes, not single quotes. Numbers must be written in decimal notation. Each property of an object must be in the form of "key": value. Property names must be string literals enclosed in double quotes. Special JavaScript syntax, such as methods, is not allowed because methods are functions, and functions are not valid JSON data types.
Top answer
1 of 7
2

Lean towards option 1, as it's a more expected format.

Option 1 works with JSON as it's designed to be used and therefore benefits from what JSON offers (a degree of human readability, which is good for debugging, and straightforward parsing, which is good for limiting entire categories of bugs to begin with).

Option 2 begrudgingly adopts JSON and subverts many of the benefits. If you don't want human readability, use protobuf or something similar... AIWalker's "CSV"-like approach isn't terrible either. It is marginally better (readable) than splitting objects apart and recombining them. But, this is still not as good (readable) as using JSON "as designed".

Also bear in mind, your API responses are also likely going to be gzipped. Most of the repetition in option 1 will be quickly and transparently condensed over the wire.

As an aside, if you're moving a lot of data, also consider JSONL or paginated results. Pagination can be especially helpful for web clients, as it places natural pauses in the processing, providing a degree of "organic" protection against UI lockups.

2 of 7
3

A list of objects is easier to work with. You can use append, map, filter... All the nice things JS Arrays have which manual indexing doesn't. And there's no way to get out of sync, so that's an entire class of bugs gone.

If you're worried about efficiency:

  • Measure (premature optimization is the root of all evil)
  • Consider the list of lists trick AIWalker proposed
  • Consider an outright binary format
  • Make sure gzip is enabled
  • Measure (it's worth saying twice)
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON
JSON - JavaScript | MDN
All properties and methods of JSON are static (just like the Math object). JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null. It is based upon JavaScript syntax, but is distinct from JavaScript: most of JavaScript is not JSON.
๐ŸŒ
Microverse
microverse.org โ€บ home โ€บ blog โ€บ how to loop through the array of json objects in javascript
How to Loop Through the Array of JSON Objects in JavaScript
September 29, 2022 - This tutorial will guide you on how to loop the array of JSON objects in JavaScript. Weโ€™ll explain the types of loops and how to use them.
Find elsewhere
๐ŸŒ
Shapediver
shapediver.com โ€บ blog โ€บ json-objects-explained
JSON Objects Explained!
An array is a collection or list of data. It is surrounded by square braces "[]" Typically, the JSON object will be created in a web application using Javascript. From there, the application can send the object to Grasshopper as a string (see next section). As a consequence, defining how the ...
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json.asp
JavaScript JSON
JSON makes it possible to store JavaScript objects as text. Text that defines an employees object with an array of 3 employee objects:
๐ŸŒ
JSONata
docs.jsonata.org โ€บ simple
Simple Queries ยท JSONata
Returns nothing (i.e. Javascript undefined) ... JSON arrays are used when an ordered collection of values is required. Each value in the array is associated with an index (position) rather than a name, so in order to address individual values in an array, extra syntax is required to specify ...
๐ŸŒ
PTC Community
community.ptc.com โ€บ t5 โ€บ ThingWorx-Developers โ€บ Search-through-array-of-JSON-objects โ€บ td-p โ€บ 859962
Solved: Search through array of JSON objects - PTC Community
March 7, 2023 - array.forEach(object => { // do your filtering here and array push here }); ... Services like find and filter work in ThingWorx, including ContentLoaderFunctions calls. This example works for me: let json = Resources["ContentLoaderFunctions"].GetJSON({ url: 'https://datausa.io/api/data?drilldowns=Nation&measures=Population' }); let result = { // JSON 'filtered': json.data.filter(row => (row['ID Year'] < 2015)) };
๐ŸŒ
RestfulAPI
restfulapi.net โ€บ home โ€บ json โ€บ json array
JSON Array - Multi-dimensional Array in JSON
November 4, 2023 - JSON array are ordered list of values. JSON arrays can be of multiple data types. JSON array can store string, number, boolean, object or other array inside JSON array. In JSON array, values must be separated by comma.
๐ŸŒ
Mixu
book.mixu.net โ€บ node โ€บ ch5.html
5. Arrays, Objects, Functions and JSON - Mixu's Node book
JSON.parse() can be used to convert JSON data to a Javascript Object or Array: // returns an Object with two properties var obj = JSON.parse('{"hello": "world", "data": [ 1, 2, 3 ] }'); console.log(obj.data); ... The optional space parameter in JSON.stringify is particularly useful in producing ...
๐ŸŒ
W3Resource
w3resource.com โ€บ JSON โ€บ structures.php
JSON Structures | JSON tutorial | w3resource
JSON supports two widely used (amongst programming languages) data structures. A collection of name/value pairs. Different programming languages support this data structure in different names. Like object, record, struct, dictionary, hash table, keyed list, or associative array.
๐ŸŒ
JSON
json.org
JSON
JSON is a text format that is ... Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language. ... A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative ...
๐ŸŒ
Adobe
opensource.adobe.com โ€บ Spry โ€บ samples โ€บ data_region โ€บ JSONDataSetSample.html
JSON Data Set Sample
Imagine you want to show a list of the different types of items, and under each item, you also want to list the different types of batters and toppings available. Doing that with the data set used in Example 10 would require some JavaScript logic embedded in spry attribute conditionals to control when things showed up. A simpler approach would be to use NestedJSONDataSets. In this example we use the same JSON data used in Example 10, but we will use 2 nested JSON data sets to track the "batter" and "topping" data.