1

I have a nested Json like this:

   string strJson = "{
  "Header":

    {"Version":"V0.00.01","ID":"1000","Name":"SetEnvValues"}


  ,
  "Data":

    {"Temp":0.00,"rH":0.00,"CO2":0.00,"O2":0.00 }



}";

Now I want to test if there is an Element "rH" existent. For example, if I only have in the Data Objekt one value "Temp", how can I test which values are existent? Is this possible without exception handling?

{
"Header":

{"Version":"V0.00.01","ID":"1000","Name":"SetEnvValues"}


,
"Data":

{"Temp":0.00 }



}

I tried it with count, but it seems this wont work for nested objects:

jsonReceivedEnvValues = json::parse(strJson);
int count = jsonReceivedEnvValues["Data"].count("Bla");

This returns always one, I think because it only tests the "Data" Object, and not the deeper nested ones.

selbolder
  • 487
  • 5
  • 22
  • 1
    Have a look at [this](https://stackoverflow.com/questions/45934851/c-nlohmann-json-how-to-iterate-find-a-nested-object). You can also use `find` and compare to the `end` iterator. – super Apr 16 '18 at 06:46
  • If I try this, I get the following exception: _what(): [json.exception.invalid_iterator.212] cannot compare iterators of different containers_ Code: `if(jsonReceivedEnvValues["Data"].find("Bla") != jsonReceivedEnvValues.end()) cout << "Entry Bla found!" << endl; else cout << "Entry Bla not found!" << endl;` – selbolder Apr 16 '18 at 07:43
  • 1
    That's because you are trying to compare iterators of `jsonReceivedEnvValues` and `jsonReceivedEnvValues["Data"]`. They are 2 different containers. You need to do `if(jsonReceivedEnvValues["Data"].find("Bla") != jsonReceivedEnvValues["Data"].end())` – super Apr 16 '18 at 08:58
  • Ok, I tried this on to, but it returns `true`, so I think this call is not using the find instruction. I found something else, it seems that `jsonReceivedEnvValues["Data"]["Bla"].is_null();` works, it retruns `false` if the second object is not existing in the Json document. – selbolder Apr 16 '18 at 09:30
  • 1
    `jsonReceivedEnvValues["Data"]["Bla"]` will create a new object if it does not exist. Why not just read the [documentation](https://github.com/nlohmann/json#json-as-first-class-data-type). You have all the information you need there. – super Apr 16 '18 at 09:46
  • 1
    I didn't reproduce your problem. When I invoke `jsonReceivedEnvValues["Data"].count("Bla");` and the keyword `Bla` doesn't exist under `Data` it returns 0, which is correct. Otherwise it returns 1. – pptaszni Apr 16 '18 at 10:05
  • int count = jsonReceivedEnvValues["Data"].count("Bla"); – selbolder Apr 16 '18 at 11:08
  • @super: You saved my day, in my tests I accidently created an Object with the following line: `jsonReceivedEnvValues["Data"]["Bla"].end();`Because of that all my tests with count failed! – selbolder Apr 16 '18 at 11:26

1 Answers1

4

It can be done with jsonReceivedEnvValues["Data"].count("Bla"). Make sure that you don't accidentally create the object you're looking for; for instance, jsonReceivedEnvValues["Data"]["Bla"].is_null(); creates an object ["Bla"] in ["Data"]

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
selbolder
  • 487
  • 5
  • 22