3

I want to extract multiple values pk, code, type.toStr (which type is nested) from the following JSON using JsonPath:

{
  "items": [{
      "pk": "000a1236",
      "code": "10023",
      "type": {
        "toStr": "report",
        ...
      },
      ...
    }, {
      "pk": "00a1ef09",
      "code": "20141",
      "type": {
        "toStr": "complaint",
        ...
      },
      ...
    },
    ...
  ]
}

I'm using JsonPath library and my attempts:

  • Using type.toStr (result: no output):
    • JsonPath: $.items[*]['pk','code','type.toStr']
    • Output: [{pk=000a1236, code=10023}, {pk=00a1ef09, code=20141}]
  • Using type (result: other fields of type are included too):
    • JsonPath: $.items[*]['pk','code','type']
    • Output: [{pk=000a1236, code=10023, type={toStr=report, ...}}, {pk=00a1ef09, code=20141 type={toStr=complaint, ...}}]

Is there any way to extract pk, code and type.toStr together (with single JsonPath) without including other type fields?

Arya
  • 2,809
  • 5
  • 34
  • 56
  • When you say `Is there any way to extract type.toStr without other fields?`, did you mean you only want `type.toStr` field value? If yes then try this: `$.items[*].type.toStr` – mayank bisht Dec 12 '17 at 14:15
  • @mayankbisht, I want to extract `pk`, `code` and `type.toStr` together (with single JsonPath) without including other `type` fields? – Arya Dec 12 '17 at 14:26
  • did you find any solution for this? I need to do exactly the same. – Vivek Kothari Apr 24 '18 at 15:40
  • Also faced this problem...Unfortunately I haven't found a solution. It seems to me that this is not in the specification itself – Amerousful Nov 25 '21 at 10:05
  • This seems related, and has a solution, but I am not sure if can be applied to this: https://stackoverflow.com/questions/46229072/how-do-i-extract-multiple-values-from-kubectl-with-jsonpath – nealmcb Dec 20 '21 at 00:54
  • @nealmcb Unfortunately it can't, since k8 uses its own flavor of JSONPath – The Onin Jan 30 '22 at 09:51

0 Answers0