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

How to iterate over nested json object #1561

Closed
ezamosch opened this issue Apr 7, 2019 · 6 comments
Closed

How to iterate over nested json object #1561

ezamosch opened this issue Apr 7, 2019 · 6 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@ezamosch
Copy link

ezamosch commented Apr 7, 2019

Hello.

I got json structure, which I need to parse:

{  
   "response":{  
      "count":207,
      "items":[  
         {  
            "id":262
         },
         {  
            "id":261
         }]
     }
}

In my c++ program I create json object from string with json:

using json = nlohmann::json;
json object = json::parse(json_param);

And then I need to iterate over respones.items. How propertly do this? I want to use 'for' operator:

for (auto it : object)
{
	std::cout << it["id"] << '\n';
}
@nlohmann
Copy link
Owner

nlohmann commented Apr 7, 2019

Try

for (auto& el : object["response"]["items].items())
{
  std::cout << el.key() << " : " << el.value() << '\n';
}

More on the items function: https://nlohmann.github.io/json/classnlohmann_1_1basic__json_afe3e137ace692efa08590d8df40f58dd.html#afe3e137ace692efa08590d8df40f58dd

@ezamosch
Copy link
Author

ezamosch commented Apr 7, 2019

Thx.
To get "id" from iterator I used:

for (auto& el : object["response"]["items].items())
{
  std::cout << el.value()["id"] << '\n';
}

@ezamosch ezamosch closed this as completed Apr 7, 2019
@ezamosch ezamosch reopened this Apr 7, 2019
@ezamosch
Copy link
Author

ezamosch commented Apr 7, 2019

But what if in iterator of this case I get another object?

{  
   "response":{  
      "count":207,
      "items":[  
         {  
            "id":262,
            "attachments":[{"element":"one"}, {"element":"two"}]
         },
         {  
            "id":261
            "attachments":[{"element":"three"}, {"element":"four"}]
         }]
     }
}

Thats how I organize inner iteration:

for (auto& el : object["response"]["items].items())
{
  std::cout << el.value()["id"] << '\n';
  for (auto& inner_el : el.value()["attachments"].items())
  {
    std::cout << inner_el.value()["element"] << '\n';
  }
}

@ezamosch ezamosch closed this as completed Apr 7, 2019
@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Apr 7, 2019
@michael-a-green
Copy link

Is there a way to iterate over a json object without having to know ahead of time how to move up or down the levels of hierarchy in the json object?

@nlohmann
Copy link
Owner

You could use the items() function. If this does not help, please open a new issue and describe your use case.

@mayank1608
Copy link

Is there a way to iterate Array of Objects??
for eg:
const color = [ { color: "red", value: "#f00" }, { color: "green", value: "#0f0" }, { color: "blue", value: "#00f" } ]

I have tried this way but not working:

json jsonObj;
json a = json::array();
jsonObj = json::parse(color);
for (json::iterator it = jsonObj.begin(); it != jsonObj.end(); ++it) {
	// for (auto& el : *it.items()) {
	// 	std::cout << el.key() << " : " << el.value() << "\n";
	// }
	a.push_back(it.key());	// returns index of jsonObj
	std::cout << it.key() << '\n';
}

std::cout << a << '\n';

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants