Prasad Parab
Prasad Parab

Reputation: 496

Filter names from Json object/array using jsonpath query

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

Upvotes: 2

Views: 989

Answers (1)

Kevin Perry
Kevin Perry

Reputation: 541

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

Upvotes: 3

Related Questions