0

There is a parent field called "price", but there is also a deeply-embedded child field also called "price" (first one is probably MSRP). How do I get JSONpath to only parse the parent ones?

{
 "name":"Toaster",
 "price":"19.99",
 "store":{
   "retailer":"Ants in My Eyes Johnson",
   "price":"9.99",
   "currency":"USD",
  }
 }

Using "$..price" yields:

"19.99"
"9.99"

I only want it to display

"19.99"

Not quite sure how.

Jarrett G.
  • 354
  • 3
  • 16
  • If you are only interested in the parent node, you could store an array or list of the keys already seen. If you encounter a child node whose key is already contained in the "seen" list, ignore it. – static_void_meringue Aug 07 '14 at 02:06
  • 3
    Why do you need to go through all the descendants? If you want just the root value, why don't you just use the direct path `$.price`? – Jeff Mercado Aug 08 '14 at 05:48

1 Answers1

2

Using .. in $..price means that you want to search the entire document. Using a single . instead will only search directly under the given location.

Therefore to only search directly under the root and display

"19.99"

You should use the following JSON Path

$.price

You can see this working online using the json query tool here: http://www.jsonquerytool.com/sample/jsonpathtoplevel

Duncan
  • 781
  • 6
  • 7