How can I input more than one field with the same name, like often occuring in json files
`// create an empty structure (null)
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// add a second number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.14151;
it seems as if the first is just overwritten. Am I missing something here?
`
You can't have the same field more than once in the same object. You can have them in different objects, or you can have the field be an array which contains more than one value.
just figured it out as well. never worked with json files before. Thank you!
But what I dont get then is how I can input a variable amount of fields, like in the following snippet, where there is this [{ pair of brackets and the inner number of entries is put in by a for loop
{
"imgHeight": 1024,
"imgWidth": 2048,
"objects": [
{
"label": "road",
"polygon": [
[
0,
769
],
[
5,
5
]
]
}
]
}`
[ ] is an array. { } is an object.
So your json is an object containing 3 key/value pairs, imgHeight, imgWidth, and objects.
The first two have number values, the third has an array value.
The array contains a single object.
That object contains 2 key/value fields, "label" and "polygon". The first has a string value, and the second has an array value. This array value contains two arrays, each of which contains two numbers.
{
"imgHeight": 1024,
"imgWidth": 2048,
"objects": [
{
"label": "road",
"polygon": [ [0, 769], [5, 5] ]
}
]
}
thank you. I think I understood the general structure, but how can I produce a .json file that looks like your example, but has a varying number of these number pairs? My approach was to use a for-loop, but I cannot loop inside structures like this one here
j["list"] = { 1, 0, 2 };
There are a few ways to do this. You can build up the array at compile time, and insert it in one shot:
j["list"] = {1, 0, 2};
You can build it at runtime and insert it in one shot:
auto vec = std::vector<int>{1, 0, 2};
j["list"] = vec;
You can create the JSON array and insert things into it
j["list"] = json::array()
auto &array = j["list"]
for (auto val : vec)
{
array.push_back(val)
}
Im not sure I can follow. I just cannot figure out how to typeset a structure like the one in your second last post
Are you just referring to how to create text from the json object and how to format it?
https://github.com/nlohmann/json#serialization--deserialization
I guess so. However, I cannot make sense of it right now =/
Just think of json to behave like a std::vector when it's an array and like a std::map when it's an object. It has both APIs, so push_back, insert, operator[] all work as expected.
well in a nutshell, I read in a json file into a json-object. Also I know how to access the individual elements inside this object. But I cant seem to figure out how to typeset then a file like above =/
maybe its just too late
j.dump(); produces a compact form, no extra whitespace.
j.dump(2); produces an indented form with two-space indents. You can set this value to whatever you want.
thanks but its not the indentation that puzzles me. Its the order of [{braces and how to fill it with the content
That's the thing that I explained above that you said you already knew how to do.
mh to be a little more discrete, I have the following json file (dont mind the exact numeric numbers)
```
{
"info": {},
"@converter": "tool",
"tags": [],
"@source": "",
"@date": "Tue Mar 07 17:16:10 2017",
"@version": 9,
"children": [
{
"seqname": "Desktop",
"tags": [],
"seqdir": "~/Desktop",
"comments": "",
"children": [
{
"tags": [],
"comments": "",
"imagename": "data000000002_000009066186.tiff",
"imageheight": 1080,
"imagewidth": 1920,
"children": [
{
"maxcol": 1175,
"tags": [],
"color": [
255,
0,
0
],
"trackid": "Sky_1",
"userz": 0.9,
"contourpoints": [
[
"+",
-1,
[
883,
338
],
[
1173,
313
],
[
974,
586
]
]
],
"maxrow": 588,
"mincol": 880,
"uniqueid": 32,
"minrow": 310,
"confidencevalues": [],
"type": "polygon",
"children": [],
"identity": "Sky",
"comments": ""
},
{
"maxcol": 556,
"tags": [],
"color": [
255,
170,
0
],
"trackid": "Car_1",
"userz": 0.09999999999999998,
"contourpoints": [
[
"+",
-1,
[
300,
172
],
[
554,
143
],
[
506,
316
],
[
303,
319
]
]
],
"maxrow": 321,
"mincol": 297,
"uniqueid": 33,
"minrow": 140,
"confidencevalues": [],
"type": "polygon",
"children": [],
"identity": "Car",
"comments": ""
}
],
"identity": "frame"
}
],
"identity": "seq"
}
],
"identity": "seqlist"
}
that I want to transform into the format
```
{
"imgHeight": 1080,
"imgWidth": 1920,
"objects": [
{
"label": "Sky",
"polygon": [
[
0,
769
],
[
290,
574
],
[
93,
528
],
[
0,
524
],
[
0,
448
],
[
0,
448
],
[
210,
453
],
[
511,
451
],
[
782,
459
]
]
},
{
"label": "Car",
"polygon": [
[
2047,
532
],
[
1911,
537
],
[
1828,
540
],
[
1782,
540
],
[
1794,
552
],
[
2047,
564
]
]
}
]
}
I now tried the following in a .cpp file:
``` #include "~/json/src/json.hpp"
using json = nlohmann::json;
json input, output, poly;
i >> input;
poly["polygon"] = json::array();
auto &array = poly["polygon"];
for (size_t i = 0; i < 2; ++i) {
array.push_back( input["children"][0]["children"][0]["children"][i]["contourpoints"][0]);
}
output["imgHeight"] = input["children"][0]["children"][0]["imageheight"];
output["imgWidth"] = input["children"][0]["children"][0]["imagewidth"];
output["objects"] = {{{"label", ""}, {"polygon", {poly}}}};
std::ofstream o("input.json");
o << std::setw(4) << output << std::endl;
return 0;
}
```
but ended up with this
{
"imgHeight": 1080,
"imgWidth": 1920,
"objects": [
{
"label": "",
"polygon": [
{
"polygon": [
[
"+",
-1,
[
883,
338
],
[
1173,
313
],
[
974,
586
]
],
[
"+",
-1,
[
300,
172
],
[
554,
143
],
[
506,
316
],
[
303,
319
]
]
]
}
]
}
]
}
close but not quite ok, also the number of contourpoint tuples can vary
Please use proper Markdown syntax. Add ``` (three backticks) before and after each code snippet.
Otherwise, it is hard to parse your examples.
I hope I set it correctly now ;and would appreciate help
I was stupid, sorry for my annoying questions. Thats what happens if one doesnt stay in bed with fever.
Everything is clear now, topic can be closed
Thanks for checking back!