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.

Answer from svidgen on Stack Exchange
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)
🌐
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.
Discussions

c# - List of Objects To Json String - Stack Overflow
How do you turn a list of Objects into a JSON String? The code below returns only one attribute, People. How to add multiple attributes to it? I have been using JsonConvert to change an object int... More on stackoverflow.com
🌐 stackoverflow.com
How to create a list of map objects within a JSON object using for....in loop
We are trying to create below mentioned JSON in terraform. Basically list of map objects Currently we are trying to use for…in loop which is iterating over a list of map (i.e. list(map(string)). Can someone please share some code for dynamically generating this list using inline for loop ? More on discuss.hashicorp.com
🌐 discuss.hashicorp.com
0
December 18, 2023
data structures - How to represent a set in JSON? - Software Engineering Stack Exchange
I thought about several ways to represent a set in JSON: ... However, a list has its own ordering, so the following two lists, ["a", "b"] and ["b", "a"] are not equal as lists, but they should be equal as sets. ... Use the key-set of the map, and ignore the values. But again, using standard comparison, the two are not the same as maps: ... This way, under standard comparison tools, the objects ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
How can I turn JSON String into a list of objects based on number of items. - Unity Engine - Unity Discussions
Hey there, I am receiving a JSON string from the backend of my webserver. I managed to create a class for User profile and mapped it with the exact fields as its JSON string. Unfortunately the Items JSON string has nested values. How can I create an object based on the number of items in my ... More on discussions.unity.com
🌐 discussions.unity.com
0
May 15, 2020
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › JSON
Working with JSON - Learn web development | MDN
For non-primitives, JSON can contain object literals and arrays, but not functions or any other object types, such as Date, Set, and Map. 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.
🌐
HashiCorp Discuss
discuss.hashicorp.com › terraform
How to create a list of map objects within a JSON object using for....in loop - Terraform - HashiCorp Discuss
December 18, 2023 - We are trying to create below mentioned JSON in terraform. Basically list of map objects Currently we are trying to use for…in loop which is iterating over a list of map (i.e. list(map(string)). Can someone please shar…
Top answer
1 of 2
41

Well, you can't. As you said, you can represent arrays and dictionaries. You have two choices.

Represent the set as an array. Advantage: Converting from set to array and back is usually easy. Disadvantage: An array has an implied order, which a set doesn't, so converting identical sets to JSON arrays can create arrays that would be considered different. There is no way to enforce that array elements are unique, so a JSON array might not contain a valid set (obviously you could just ignore the duplicates; that's what is likely to happen anyway).

Represent the set as a dictionary, with an arbitrary value per key, for example 0 or null. If you just ignore the values, this is a perfect match. On the other hand, you may have no library support for extracting the keys of a dictionary as a set, or for turning a set into a dictionary.

In my programming environment, conversion between set and array is easier (array to set will lose duplicate values, which either shouldn't be there, or would be considered correct), so for that reason I would go with arrays. But that is very much a matter of opinion.

BUT: There is a big fat elephant in the room that hasn't been mentioned. The keys in a JSON dictionary can only be strings. If your set isn't a set of strings, then you only have the choice of using an array.

2 of 2
12

Don't try to represent sets in JSON. Do it when parsing the data instead.

Your JSON data should have a schema which specifies which fields should be treated as a set, or you may have a metadata embedded in the JSON data itself that describes when a list should be treated as a set (e.g. {"houses": {"_type": "set", "value": [...]}}) or with a naming convention.

Note that according to JSON standard, a JSON object can have duplicate keys. ECMA-404 wordings:

Objects

[...] The JSON syntax does not impose any restrictions on the strings used as names, does not require that name strings be unique, and does not assign any significance to the ordering of name/value pairs. These are all semantic considerations that may be defined by JSON processors or in specifications defining specific uses of JSON for data interchange.

AFAICD, nothing in the spec forbids non unique names, and there are many JSON parser implementations that can parse non unique object names. RFC 7159 discourages non unique names for interoperability, but specifically don't forbid it either, and goes on to list how various parsers have been seen handling non unique object names.

And ECMA 404 also doesn't require that array ordering be preserved:

Arrays

The JSON syntax does not define any specific meaning to the ordering of the values. However, the JSON array structure is often used in situations where there is some semantics to the ordering.

This wording allows applications to use arrays to represent sets if they so choose.

Find elsewhere
🌐
DataCamp
datacamp.com › tutorial › json-data-python
Python JSON Data: A Guide With Examples | DataCamp
December 3, 2024 - While Python is capable of storing intricate data structures such as sets and dictionaries, JSON is limited to handling strings, numbers, booleans, arrays, and objects. Let’s look at some of the differences: To convert a Python list to JSON format, you can use the json.dumps() method from ...
🌐
Unity
discussions.unity.com › unity engine
How can I turn JSON String into a list of objects based on number of items. - Unity Engine - Unity Discussions
May 15, 2020 - Hey there, I am receiving a JSON string from the backend of my webserver. I managed to create a class for User profile and mapped it with the exact fields as its JSON string. Unfortunately the Items JSON string has neste…
🌐
JSON
json.org
JSON
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
🌐
YouTube
youtube.com › watch
Storing Data (Objects & Lists) with JSON - Unity Tutorial 2021 - YouTube
In this tutorial you'll learn how to save and read data into JSON files. We focuse more on lists which needs a little bit more code than default objects, but...
Published   May 17, 2021
🌐
RPTools.net
forums.rptools.net › viewtopic.php
collect all values in a json object? - RPTools.net
There are some perfectly useable examples earlier in this thread for doing either operation individually, but my original request was just for the list of values, not the end product, which can vary depending on the specific case. As another example, I may have an object which keeps track of items equipped to body slots ... [H: BodySlots = json.set("", "Head", "Merlin's Beard", "Body", "Robe of the Archmagi", "Hand", "Staff of Napalm", "Off-Hand", "The One Ring")]Each item is assigned to a body slot, because I want to make sure some things are equipped in appropriate places and that I'm not equipping too many things in a limited number of slots.
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › array
JSON Schema - array
Arrays are used for ordered elements. In JSON, each element in an array may be of a different type. Language-specific info: Python · Ruby · Objective-C · Swift · In Python, "array" is analogous to the list or tuple type, depending on usage. However, the json module in the Python standard library will always use Python lists to represent JSON arrays.
🌐
Reddit
reddit.com › r/dotnet › trying to deserialize a json into a list of objects so that they can be passed on to a view.
r/dotnet on Reddit: Trying to Deserialize a JSON into a list of Objects so that they can be passed on to a view.
July 29, 2022 -

Let me prefix this by saying I am very new to all this so any help would be so very appreciated - i've been trying to crack this all day and it's been a real struggle. Im making this webapp in ASP.NET core btw. So I've managed to call the twitter api and return back some tweets as JSON depending on a search query. I am then trying to deserialize these into C# objects (using the Tweet class). I then want to pass these objects through as a list to the view for it to render them all out But I just can't seem to get it to work.

I am trying to put desrialize the JSON that comes back into Tweet objects and put them into a ViewModel and then pass the viewmodel to the view. This is what comes back from the API call so I know thats working ok. This is what the Tweet class looks like. The SocialMediaType property is a class in itself, and just has two properties on it which are an Id and name which are given default values as they will always be the same. The error page
🌐
Adobe
opensource.adobe.com › Spry › samples › data_region › JSONDataSetSample.html
JSON Data Set Sample
The examples on this page attempt to illustrate how the JSON Data Set treats specific formats, and gives examples of the different constructor options that allow the user to tweak its behavior. See our JSON Primer for more information. Example 1 - JSON Array with simple data types as elements. ... Example 4 - The "path" constructor option. Example 5 - The "path" constructor option and JSON Array with objects as elements.
🌐
Micro Focus
microfocus.com › documentation › silk-performer › 205 › en › silkperformer-205-webhelp-en › GUID-0847DE13-2A2F-44F2-A6E7-214CD703BF84.html
JSON Array Structure
In contrast to regular arrays from the BDL, the elements of a JSON array can be of different data types. The following data types are allowed for JSON arrays: [ ] //Empty JSON array [ 0, 1, 2, 3, 4, 5] [ “StringValue”, 10, 20.13, true, null ] [ { “Name” : “Nested Object” }, [ 10, ...
🌐
Reddit
reddit.com › r/learnpython › list all json keys in a file to identify database column names from a file using python
r/learnpython on Reddit: List all JSON keys in a file to identify database column names from a file using Python
July 9, 2022 -

I am learning Python, and in particular, working with JSON and sqlite in Python. Ultimately I plan to use Python to load the JSON into a sqlite database.

Here is the question: Is there a way in to list all of the keys from a JSON file (not from a string) using Python? I want a list of all of the keys so I can determine what columns I will need/use in my sqlite table(s), without having to manually read the file and make a list.

BTW, this is something along the lines of using INFORMATION_SCHEMA.COLUMNS in SQL Server, or the FINDALL in Python for XML.

All of this is for personal learning, so I'm not looking to use other technologies, I'm sticking to Python, JSON, and sqlite on purpose.

🌐
JAXB
javaee.github.io › tutorial › jsonp001.html
Introduction to JSON
JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values.