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 find subelement without loop #1490

Closed
bnadem opened this issue Feb 21, 2019 · 6 comments
Closed

how to find subelement without loop #1490

bnadem opened this issue Feb 21, 2019 · 6 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@bnadem
Copy link

bnadem commented Feb 21, 2019

i hae normal json file structure like

{
"element" :{
"test":"test",
"subelement": {
      "subsubelemnt1":"githu",
        "subsubelemnt2":"githu",
         "subsubelemnt3":"githu",
          "subsubelemnt4":"githu",

}
}
}

want to find "subelement2" or"subelement""subelement2" without looping, is there function like find , exist, contain without iterating?

  • What is the issue you have?

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

  • What is the expected behavior?

  • And what is the actual behavior instead?

  • Which compiler and operating system are you using? Is it a supported compiler?

  • Did you use a released version of the library or the version from the develop branch?

  • If you experience a compilation error: can you compile and run the unit tests?

@nickaein
Copy link
Contributor

nickaein commented Feb 21, 2019

You can use JSON Pointers to access an element if you can provide its full address in the hierarchy of JSON document e.g. json_doc["/element/subelement/subsubelemnt2"].

I'm not sure whether there is a method to look up for subsubelemnt2 in all subtrees.

@bnadem
Copy link
Author

bnadem commented Feb 22, 2019

no dont working, i become null pointer.

@nlohmann
Copy link
Owner

Assuming your JSON value is j, then you can access the value with j["element"]["subelement"]["subsubelemnt1"];.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Feb 22, 2019
@garethsb
Copy link
Contributor

You can use JSON Pointers to access an element if you can provide its full address in the hierarchy of JSON document e.g. json_doc["/element/subelement/subsubelemnt2"].

json_doc["/element/subelement/subsubelemnt2"_json_pointer]

@nickaein
Copy link
Contributor

@garethsb-sony: Whoops. Thanks for clarification!

@garethsb
Copy link
Contributor

@nickaein I just didn't want anyone to think the json_pointer solution didn't work. :-)

@nlohmann nlohmann closed this as completed Mar 8, 2019
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