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

accessing json object as multimap #1892

Closed
Prati369 opened this issue Jan 7, 2020 · 4 comments
Closed

accessing json object as multimap #1892

Prati369 opened this issue Jan 7, 2020 · 4 comments
Labels
state: needs more info the author of the issue needs to provide more details state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@Prati369
Copy link

Prati369 commented Jan 7, 2020

I have a json file in this format. I want to access it . Ex: for key 'f' output should be 1. I tried many ways but unable to access it directly.

JSON file:
{
"1": "a", "2": [{ "a": [{ "b": [{ "c": [{ "d": [{ "e": { "f": "1", "g": "2" }, "h": [{ "i": "1", "j": "2" }], "j": { "l": "1", "m": 2 }, "n": { "o": "1", "p": 2 } }] }], }] }] } }

@Prati369 Prati369 closed this as completed Jan 7, 2020
@Prati369 Prati369 reopened this Jan 9, 2020
@nlohmann
Copy link
Owner

nlohmann commented Jan 9, 2020

The value you posted is not valid JSON. Could you please double check?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Jan 9, 2020
@dota17
Copy link
Contributor

dota17 commented Jan 14, 2020

Is ] missing before the last one '}'?
I added ']' there, and it would be fine:

{
    "1": "a",
    "2": [
        {
            "a": [
                {
                    "b": [
                        {
                            "c": [
                                {
                                    "d": [
                                        {
                                            "e": {
                                                "f": "1",
                                                "g": "2"
                                            },
                                            "h": [
                                                {
                                                    "i": "1",
                                                    "j": "2"
                                                }
                                            ],
                                            "j": {
                                                "l": "1",
                                                "m": 2
                                            },
                                            "n": {
                                                "o": "1",
                                                "p": 2
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

@Prati369, is this right?

@nickaein
Copy link
Contributor

Possibly relevant to #1141.

You can f by chaining its parents, e.g.:

std::cout << json_object["2"]["a"]["b"]["c"]["d"]["e"]["f"]

or use JSON pointers

std::cout << json_object["/2/a/b/c/d/e/f"_json_pointer]

If the object hierarchy is not known beforehand, you can traverse the JSON object and look for the key. The following function is one simple way of doing that. Here is the working example: https://wandbox.org/permlink/Hlg3KIpxTSCdSPM8

nlohmann::json find_child_recursive(const nlohmann::json& j, std::string query_key)
{
    if(j.is_object())
    {
        for(const auto& [key, value]: j.items())
        {
            if(key == query_key)
            {
                return value;
            }
            else if(value.is_object() || value.is_array())
            {
                return find_child_recursive(value, query_key);
            }
        }
    }
    else if(j.is_array())
    {
        for(const auto& item: j)
        {
            return find_child_recursive(item, query_key);
        }
    }
    
    return nullptr;
}

@stale
Copy link

stale bot commented Feb 15, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Feb 15, 2020
@stale stale bot closed this as completed Feb 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs more info the author of the issue needs to provide more details state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

4 participants