Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to select all child array with just JsonPath

Tags:

json

jsonpath

I have a json as below

{
    "Shop": [
      {
        "id": "1",
        "Items": [
          {
            "Item": "Item1"
          }
        ]
      },
      {
        "id": "2",
        "Items": [
          {
            "Item": "Item2"
          }
        ]
      },
      {
        "id": "3",
        "Items": [
          {
            "Item": "Item3"
          }
        ]
      }
    ]
        
}

I would like to select all Items with just JsonPath. I have tried as following combinations but I did not get any values

$.[Shop[0], Shop[1], Shop[2]].Items

$.[Shop[0].Items, Shop[1].Items, Shop[2].Items]

Thank you in advance

like image 736
Kerberos Avatar asked Oct 22 '25 14:10

Kerberos


2 Answers

If I understand correctly you are looking for * wildcard to select all elements in an array:

$.Shop[*].Items

Gives me:

[ [ { "Item": "Item1" } ], [ { "Item": "Item2" } ], [ { "Item": "Item3" } ] ]

like image 133
wp78de Avatar answered Oct 25 '25 07:10

wp78de


Using jsonpath-cli:

$.Shop[*].Items[*]

gives

[{"Item":"Item1"},{"Item":"Item2"},{"Item":"Item3"}]

Alternatively,

$.Shop[*].Items.['Item']

gives

["Item1","Item2","Item3"]
like image 42
CoatedMoose Avatar answered Oct 25 '25 07:10

CoatedMoose