1

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

wp78de
  • 18,207
  • 7
  • 43
  • 71
Kerberos
  • 1,228
  • 6
  • 24
  • 48

1 Answers1

1

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" } ] ]

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Thank you for your support but that gives 3 different arrays. I would like to merge result in one array. I mean I do not want to run nested for-each in business logic. – Kerberos Oct 12 '20 at 14:38
  • @Kerberos you can simply flatten the result array `[..].flat()`. Deal done. – wp78de Oct 12 '20 at 14:45
  • OK but I think flat() is not a part of xpath? – Kerberos Oct 12 '20 at 14:49
  • My question about xpath. It is possible or not? – Kerberos Oct 12 '20 at 14:50
  • I don't think so. If you are concerned about optimizations like that you may want to stay away from JSONPath altogether and process the JSON using your general language. – wp78de Oct 12 '20 at 15:04
  • 1
    Comparable https://stackoverflow.com/questions/54819775/how-to-flatten-a-json-with-nested-lists-by-jayway-jsonpath – wp78de Oct 12 '20 at 15:35