The second is almost correct:
{
"foos" : [{
"prop1":"value1",
"prop2":"value2"
}, {
"prop1":"value3",
"prop2":"value4"
}]
}
Answer from Justin Niessner on Stack OverflowThe second is almost correct:
{
"foos" : [{
"prop1":"value1",
"prop2":"value2"
}, {
"prop1":"value3",
"prop2":"value4"
}]
}
The first example from your question,
foos: [ foo: { ... }, foo: { ... } ]
is in invalid syntax. You cannot have object properties inside a plain array.
The second example from your question,
foos: [ { ... }, { ... } ]
is right although it is not strict JSON. It's a relaxed form of JSON wherein quotes in string keys are omitted.
Following is the correct one when you want to obey strict JSON:
"foos": [
{ ... },
{ ... }
]
This "Mastering JSON" tutorial by Patrick Hunlock, may help to learn about JSON and this site may help to validate JSON.
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.
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)
I have a sqlite database were I store my workout exercises. I have been working to get these exercises into a JSON files to send to my website to display graphs using Chartjs.
Going through this article (https://changsin.medium.com/how-to-serialize-a-class-object-to-json-in-python-849697a0cd3) they take two objects and turn them into a JSON output. Like this:
label1 = Label("person", 10, 10, 4, 10)
label2 = Label("car", 20, 20, 5, 11)
image_bboxes = {"image1.jpg": [label1, label2]}
output:
{"version": 1,
"type": "box",
"bounding box":
{"image1":
[{"label": "person",
"x": 10,
"y": 20
},
{"label": "car",
"x": 10,
"y": 20
}
]
}
}Now I have a list of lists looking like this:
all_exercises = [[planks1, planks2],[bench1,bench2]]
I want to create a JSON output that will allow me to pass it off to my website. Then use JS and Chartjs to display the user selected graph. I can get the graph to do what I want it to do. I don't know how to make the JSON output. I'm not understanding something fundamental when I am using JSON. I just don't know what it is. A book or resource would be greatly appreciated.
My output currently looks like this:
{
"version": 1,
"exertest": {
"second": [
{ //Need's to be labeled PLANK here
"id": 325,
"interval": 1,
"resistance": 0,
"set_number": 1,
"workout_id": 58,
"workout_date": "2022-03-28T06:00",
"exercise_id": 24,
"exercise_name": "Plank"
},
{
"id": 326,
"interval": 1,
"resistance": 0,
"set_number": 2,
"workout_id": 58,
"workout_date": "2022-03-28T06:00",
"exercise_id": 24,
"exercise_name": "Plank"
},
{ // Needs to be labeled HIP BRIDGE here
"id": 327,
"interval": 15,
"resistance": 0,
"set_number": 1,
"workout_id": 58,
"workout_date": "2022-03-28T06:00",
"exercise_id": 25,
"exercise_name": "Hip Bridge"
}
]
}
}