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

Confused as how I can extract the values from the JSON object. #2055

Closed
jcracine opened this issue Apr 20, 2020 · 4 comments
Closed

Confused as how I can extract the values from the JSON object. #2055

jcracine opened this issue Apr 20, 2020 · 4 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@jcracine
Copy link

  • Describe what you want to achieve.
    I want to retrieve data from a JSON object.
    Ex:
{
    "results": [
        {
            "series": [
                {
                    "columns": [
                        "time",
                        "value"
                    ],
                    "name": "test",
                    "values": [
                        [
                            1587405996298,
                            10
                        ],
                        [
                            1587406122630,
                            10
                        ]
                    ]
                }
            ],
            "statement_id": 0
        }
    ]
}
  • Describe what you tried.
    I've read the README section to understand better how to extract data from a JSON object, but I'm still confused on how I can do so. I tried at() or get() but can't seems to get to easily retrieve values like "name" or "columns".

  • Describe which system (OS, compiler) you are using.
    Linux Fedora 30 with gcc version 9.2.1 20190827 (Red Hat 9.2.1-1) (GCC)

  • Describe which version of the library you are using (release version, develop branch).
    I'm using version 3.7.3

@nlohmann
Copy link
Owner

Did you already parse the JSON?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Apr 21, 2020
@oitan
Copy link

oitan commented Apr 21, 2020

As you said I would just use json["results"].at(0)["series"].at(0)["name"];. Not sure if there are easier ways without at() or get() methods. To make it more accessible you could use object conversion from this example. I hope it helps.

@jcracine
Copy link
Author

jcracine commented Apr 21, 2020

@nlohmann Did you already parse the JSON?

Yes I did.

@oitan As you said I would just use json["results"].at(0)["series"].at(0)["name"];. Not sure if there are easier ways without at() or get() methods. To make it more accessible you could use object conversion from this example. I hope it helps.

I think the object conversion could do the trick. Now that I read it and tested it more, I think I have a better grasp on how to extract the data.

@nlohmann
Copy link
Owner

You can also use operator[] for arrays, so instead of

json["results"].at(0)["series"].at(0)["name"];

you can write

json["results"][0]["series"][0]["name"];

The get() function is only needed if you want to explicitly state the type you want to get. You could also do it this way:

std::string name = json["results"][0]["series"][0]["name"];

@jcracine Do you need further assistance?

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed state: needs more info the author of the issue needs to provide more details labels Apr 22, 2020
@nlohmann nlohmann closed this as completed May 1, 2020
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