Working with JSON responses in Velocity

We've got a webservice that's returning a JSON result.. however, I'm struggling to get to data within the JSON response using Velocity. I can see it's a JSON object in the sandbox, and I've tried using ParseJSON to convert it to an object.. but I then can't do anything with that object.

Here's some sample code (I've put the JSON into a string for this example so it doesn't depend on the webservice);

#set($responseText = '{ "responseHeader":{ "zkConnected":true, "status":0, "QTime":82, "params":{ "q":"*:*", "indent":"true", "rows":"0", "wt":"json"}}, "response":{"numFound":653874,"start":0,"maxScore":3.5213852,"docs":[] }}')
responseText: $responseText<br>
responseText.response: $responseText.response<br>

#set($responseObj = $core_v2_utility.ParseJson($responseText))
responseObj: $responseObj
 

When this is run in the sandbox, it correctly shows the string is JSON (just like it does when it's got the payload from the actual webservice);

How do I get down into the JSON and pull out something like 'numFound' ? 

Parents Reply
  • Jeez, how embarrassing Flushed Yup, you're quite right. I'd tried a billion combinations, and looking back at my tests (using the invaluable TimeSnapper, which screenshots every 5 seconds so that I can go back and look at what I was doing) I saw I'd misspelled '.response' and assumed it wasn't working. Sigh.

    Thanks for confirming!

    That's got my Search Index status panel working nicely (it needed to parse the response from the Solr API).

Children