JSONPath first occurrence of "gatherer"

159 views Asked by At

I have a json file like this:

{
  "cards": [
    {
      "artist": "Steve Argyle",
      "images": {
        "mtgimage": "http://mtgimage.com/set/pMPR/ponder.jpg"
      },
    },
    {
      "artist": "Mark Tedin",
      "images": {
        "gatherer": "http://gatherer.wizards.com/Handlers/Image.ashx?type=card&multiverseid=139512",
        "mtgimage": "http://mtgimage.com/set/LRW/ponder.jpg"
      },
    },
    {
      "artist": "Dan Scott",
      "images": {
        "gatherer": "http://gatherer.wizards.com/Handlers/Image.ashx?type=card&multiverseid=190159",
        "mtgimage": "http://mtgimage.com/set/M10/ponder.jpg"
      },
    }
  ]
}

I would like to get the first "gatherer" link from it using JSONPath. I tried "$..gatherer[0]" but that doesn't work. However "$..gatherer" does give both instances:

[
   "http:\/\/gatherer.wizards.com\/Handlers\/Image.ashx?type=card&multiverseid=139512",
   "http:\/\/gatherer.wizards.com\/Handlers\/Image.ashx?type=card&multiverseid=190159"
]

How do I get only the first one? (without getting the last one in code, so only using a jsonpath string.)

(Tested with http://jsonpath.curiousconcept.com/ and in my program.)

1

There are 1 answers

1
Toby Allen On BEST ANSWER

You may need an interim step to save the array

var gatherers = $..gatherer;  //however this gets it, I'm not sure
var first = gatherers[0];

that may work.