Json: nesting json objects

Created on 11 Sep 2017  Â·  10Comments  Â·  Source: nlohmann/json

RE: Nesting json objects

Is there a way to directly nest json objects in similar manner?

json outerObj = { "misting" , { "isMisting" , true } };
json nestedObj = { "duration" , { "hours" , 2}, { "mins" , 30 }, {"secs" , 20 } };
json result = outerObj[ nestedObj ]; // does not work

To obtain this:-
image

What is most direct way to achieve this?

Thanks.

question proposed fix

All 10 comments

outerObj[ "duration" ] = nestedObj;
result is outerObj itself.

BTW, you should ask it in Stack Overflow not here.

An object stores key/value pairs. In your example nestedObj is not a string, so this can't work.

Try this:

json j_duration = { {"hours", 17}, {"mins, 18}, {"secs", 19} };
json j_misting = { {"isMisting", true}, {"duration", j_duration} };
json result = { {"misting, j_misting } };

@leozzyzheng No, this is the right place to ask, see https://github.com/nlohmann/json/issues?utf8=✓&q=label%3A%22kind%3A%20question%22%20.

@nlohmann Oh, sorry, I usually think report bugs or ask features could open an issue. Ask how to use should read document first. Just my view:)

I know, but some people are quite active here, so I don't bother having questions here.

Yeah, I see them :)

@sqwunkly Does this work for you?

@nlohmann @leozzyzheng Yes, I think so.. it's still early stages but I think I can achieve all the building of nested json objects using your square brackets notation.

This code below works well for converting and storing my json data into an array of corresponding nlohmann_json objects. It's not a great example of deep nesting but as far as I can tell any level of nesting can be achieved in this manner.

So, unless I am mistaken, as long as I have the "_name_" (as _variable char array_, or as _string literal_) I'll be able to use this notation:-

j_object[_name_1_][_name_2_][_name_3_} = j_object_to_be_nested

for building nested objects to any desired depth. Now that I understand it this method is neat and succinct and I am happy with it. Niels, as I get further I'll let you know how I go. Much thanks!

image

@leozzyzheng It may have seemed to you like I didn't read the docs for my question was basic, however I had spent a lot of time (a few days) reading the documents and googling. I've not done much C++ programming for 10 years and I've done zero modern c++ . Even after doing my utmost to understand the docs much still wasn't clear to me and I needed some experienced help. I do appreciate your's and Niel's. Cheers.

You can simplify your for loop as follows:

for (auto it = j_program->begin(); it != j_program->end(); ++it)
{
  j_sessionsArray[i][it.key()] = it.value();
  ++i;
  ++sessionsCount;
}
  • The [] operator works with strings, so there is no need to call c_str().
  • it.value() gives you the value for the element with key it.key().

that is much better.. thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Prati369 picture Prati369  Â·  4Comments

zhishupp picture zhishupp  Â·  4Comments

bassosimone picture bassosimone  Â·  3Comments

alienzj picture alienzj  Â·  4Comments

sqwunkly picture sqwunkly  Â·  3Comments