Json: accessing json object as multimap

Created on 7 Jan 2020  路  4Comments  路  Source: nlohmann/json

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 } }] }], }] }] } }

needs more info stale

Most helpful comment

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;
}

All 4 comments

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

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?

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;
}

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asmaloney picture asmaloney  路  4Comments

bassosimone picture bassosimone  路  3Comments

zkelo picture zkelo  路  3Comments

garethsb-sony picture garethsb-sony  路  4Comments

zhishupp picture zhishupp  路  4Comments