0

I am using JSONPath to filter out a json file, I am trying to retrieve both the Key + Value from the JSON array but I am lost at figuring how best to do this.

QUERY

$.phoneNumbers[*].[type,number]

ARRAY

{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
  "streetAddress": "naist street",
  "city"         : "Nara",
  "postalCode"   : "630-0192"
 },
 "phoneNumbers": [
  {
     "type"  : "iPhone",
     "number": "0123-4567-8888",
     "address"  : {
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
  }
},
{
  "type"  : "home",
  "number": "0123-4567-8910",
  "address"  : {
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
   }
  }
 ]
}

CURRENT OUTPUT

[
  "iPhone",
  "0123-4567-8888",
  "home",
  "0123-4567-8910"
]

Desired Outcome

[
 {
   "type"  : "iPhone",
   "number": "0123-4567-8888",
 },
 {
   "type"  : "home",
   "number": "0123-4567-8910",
 }
]
Kyle McBride
  • 171
  • 1
  • 8
  • 1
    The original blog post says that an implementation should be able to return either the value or the path to it, but not necessarily both. Also, there's not a formal spec (though we're writing one), so implementations vary in their support. You'll need to check with the implementation you're using. – gregsdennis Dec 17 '21 at 19:20
  • Currently Jayway implementation returns both key and values not the Goesnner implementation. try it in https://jsonpath.herokuapp.com/ – Akshay G Dec 18 '21 at 08:12

2 Answers2

1

What you're looking for can't be done with JSON Path because you're looking to transform the data. JSON path is only going to give you the values in a flat list.

You might find some luck with JMES Path. It's pretty similar, but I'm less familiar with it.

gregsdennis
  • 7,218
  • 3
  • 38
  • 71
0

You likely need to add quotes

$.phoneNumbers[*].['type','number']
gregsdennis
  • 7,218
  • 3
  • 38
  • 71
  • That’s for Jayway jsonpath implementation – Akshay G Dec 18 '21 at 08:08
  • @AkshayG I'm not sure I understand your comment. This is valid in many implementations. See [JSON Path comparison](https://cburgmer.github.io/json-path-comparison/) (search for `Union with keys`) and specifically [`Union with keys`](https://cburgmer.github.io/json-path-comparison/results/union_with_keys.html). – gregsdennis Dec 19 '21 at 10:55
  • I did not say it’s not valid. I said the OPs query is also valid in orignal proposed goesnner implementation. There is no need to add quotes when the keys are one word. However for Jayway you always need to mention the keys within quotes. – Akshay G Dec 19 '21 at 14:50
  • Also the OP has clearly mentioned the output of the query and did not mention of syntax error . – Akshay G Dec 19 '21 at 14:58
  • Quotes are required for bracket syntax, but not for dot syntax. – gregsdennis Dec 20 '21 at 00:44
  • The output was added at my request after I added this answer. – gregsdennis Dec 20 '21 at 00:45