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

Best method for exploring and obtaining values of nested json objects when the names are not known beforehand? #747

Closed
sqwunkly opened this issue Sep 18, 2017 · 6 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@sqwunkly
Copy link

Hi Niels,

Could you please advise me the best way to explore down into a list of JSON objects, without knowing the names of the nested objects beforehand.

I'm using the notation below (1 & 2) for setting and getting values and this works well, however I've not been able to find a way of quickly getting the names and values of objects when I don't know the names of those objects beforehand. I've used the iterator method (3) however it doesn't seem suited for quickly diving and exploring into nested objects.

What is the most direct way of exploring and getting values of nested objects when the names of those objects are not known beforehand?

(1)
image

(2)
image

(3)
image

@sqwunkly
Copy link
Author

sqwunkly commented Sep 19, 2017

Using a pointer seems promising however I'm unable to achieve what I need.

Below is the code I'm using to get a pointer to the first object ("group 1") inside the json file loaded and read into my program. Using the pointer I need to get the names and values of the nested 'schedules' objects inside "group 1", "group 2", etc.. however I don't know beforehand the 'schedules' names.. these 'schedules' could for example be named "Morning Schedule", "Afternoon Schedule", "Rainy Day Schedule", etc or something else. ... thus, the 'schedules' names are unknown.

image

Debugger view is below:- I have the p1 pointer.. it's pointing to "group 1" (obtained in code above)... using this p1 pointer I now need to locate "Schedule1", "Schedule2", etc and obtain their names (ie unknown arbitrary names) and read their unknown values (the structure of the 'schedules' data is consistent and is known) into a corresponding linked list of data.

image

After much trying, I'm unable to access the nested 'schedules' keys (of unknown arbitary names) using the p1 pointer.. can you please explain how to do this, or advise better way to do this? Much thanks.

@sqwunkly
Copy link
Author

Explicitly showing these arbitarily named 'schedules' objects:-

image

How do I access these in a succinct manner without knowing their key names beforehand?

Thanks.

@sqwunkly
Copy link
Author

Hi Niels, This works below however I'm wondering, is there a more direct and succinct way of querying nested keys and obtaining their values of unknown 'key' name'?

image

@gregmarr
Copy link
Contributor

This example should help make your for loop cleaner, but you have the basic idea. Just make sure that you have the right types when accessing things, as in your other issue. Use auto wherever possible so that you don't silently change the type.

https://github.com/nlohmann/json/blob/develop/doc/examples/iterator_wrapper.cpp

@nlohmann
Copy link
Owner

Can I close this issue?

@sqwunkly
Copy link
Author

sqwunkly commented Sep 23, 2017 via email

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Sep 23, 2017
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

3 participants