2

I am trying to extract all name from computerScience and electronics using json path query

my input json is

{
  "computerscience": [
    {
      "name": "john doe",
      "grade": "B",
      "year": "4"
    },
    {
      "name": "Bjarne Stroustrup",
      "grade": "A",
      "year": "4"
    },
    {
      "name": "Dennis Ritchie",
      "grade": "A",
      "year": "4"
    }
  ],
  "someProp1": "false",
  "someProp2": "dirSync",
  "electronics": [
    {
      "name": "thomas edison",
      "grade": "B",
      "year": "4"
    },
    {
      "name": "nichola tesla",
      "grade": "B",
      "year": "4"
    }
  ]
}

I want to figure out json path which give me below result

["john doe", "Bjarne Stroustrup", "Dennis Ritchie" , "thomas edison", "nichola tesla"]

I want to extract names from only computerScience and electronics

I am trying few jsonpaths here but it won't work

1
  • Can you also post the few jsonpath that you have tried which won’t work? Commented Feb 7, 2022 at 22:41

1 Answer 1

3

This pattern works:

$.[computerscience,electronics][*].name

This simpler pattern works as long as there aren't any other properties that you want to avoid selecting the names from:

$.*[*].name

Or if you really just want to collect all the names in the document, there's:

$..name
Sign up to request clarification or add additional context in comments.

2 Comments

And of course I should have mentioned, these operators are documented here: github.com/json-path/JSONPath#operators
As per the documentation of Jayway JSONpath the query should look like $.['computerscience','electronics'][*].name . The names of the properties should be enclosed within single quotes. Try here jsonpath.herokuapp.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.