Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input several field with the same name #488

Closed
CDitzel opened this issue Mar 7, 2017 · 19 comments
Closed

Input several field with the same name #488

CDitzel opened this issue Mar 7, 2017 · 19 comments

Comments

@CDitzel
Copy link

CDitzel commented Mar 7, 2017

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?
`

@gregmarr
Copy link
Contributor

gregmarr commented Mar 7, 2017

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.

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

just figured it out as well. never worked with json files before. Thank you!

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

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
]
]
}
]
}`

@gregmarr
Copy link
Contributor

gregmarr commented Mar 7, 2017

[ ] 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] ]
    }
  ]
}

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

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 };

@gregmarr
Copy link
Contributor

gregmarr commented Mar 7, 2017

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)
}

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

Im not sure I can follow. I just cannot figure out how to typeset a structure like the one in your second last post

@gregmarr
Copy link
Contributor

gregmarr commented Mar 7, 2017

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

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

I guess so. However, I cannot make sense of it right now =/

@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

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.

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

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

@gregmarr
Copy link
Contributor

gregmarr commented Mar 7, 2017

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.

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

thanks but its not the indentation that puzzles me. Its the order of [{braces and how to fill it with the content

@gregmarr
Copy link
Contributor

gregmarr commented Mar 7, 2017

That's the thing that I explained above that you said you already knew how to do.

@CDitzel
Copy link
Author

CDitzel commented Mar 7, 2017

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:

using json = nlohmann::json;
#include <fstream>
#include <iostream>

 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


@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2017

Please use proper Markdown syntax. Add ``` (three backticks) before and after each code snippet.

Otherwise, it is hard to parse your examples.

@CDitzel
Copy link
Author

CDitzel commented Mar 8, 2017

I hope I set it correctly now ;and would appreciate help

@CDitzel
Copy link
Author

CDitzel commented Mar 8, 2017

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

@nlohmann
Copy link
Owner

nlohmann commented Mar 8, 2017

Thanks for checking back!

@nlohmann nlohmann closed this as completed Mar 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants