Parsing JSON format responseText - not as easy/clearcut as XML. Does it even work?

Parsing JSON format responseText - not as easy/clearcut as XML. Does it even work?

The usefulness of JSON is clear in that you can you use key-value pair 'get()' method to retrieve the value of a key...which in theory is a lot easier than used Xpath and XML...but I have found this to be difficult to resolve when using getURL or postURL and I need to parse the responseText that gets sent back.

In the case of the example I have dealt with the JSON format data, I think it is sending back a multi-dimensional array of data...and my assumptions about using get(keyname) seem to break down.

Referring to this post here - http://help.zoho.com/portal/en/community/topic/quick-tip-on-using-json-data-resources-sharing-records-between-apps I get the impression for the get() method to be useful, it can only work on map variables that are flat...

The code below illustrates...I get a null value when I try to get the string from a call to get("application-name") which is definitely the first key...my intention was to then convert the returned string, to another map variable and bit by bit get to the text I wanted. But as I said...it fails at the first hurdle.

If anyone know, please advise on how one should parse JSON format data return by postURL and/or getURL....

Code is below...

  1. ResponseMapVariable = postUrl("http://creator.zoho.com/api/json/apitite/forms/apikey=" + theAPIKey + "&ticket=" + theTicket, mapvariable,false);
  2. ResponseCode = ResponseMapVariable.get("responseCode");
  3. ResponseText = ResponseMapVariable.get("responseText");
  4. thisapp.globalvars.SetWidelyUsedTestVariable("Post URL JSON Tests - ResponseCode and ResponseText are : " + ResponseCode + "   " + ResponseText);

  1. //
  2. // Using XML to decode the response data works fine...
  3. // This code was got from here - http://help.creator.zoho.com/Get-Data-From-External-Website.html
  4. //
  5. info "These are the XML parsing results....";
  6. xmldata = ResponseText.toXML();
  7. strdata = xmldata.executeXPath("/root/application-name/formList");
  8. xmllist = strdata.toXmlList();
  9. info "Number of Apps is : " + xmldata.executeXPath("/root/application-name/formList/formCount/text()");
  10. for each r in xmllist
  11. {
  12.     info "App name : " + r.executeXPath("/formList/componentname/text()");
  13.     info "Display name : " + r.executeXPath("formList/displayname/text()");
  14.     info "Link ID : " + r.executeXPath("/formList/linkid/text()");
  15. }

  16. // ALAS MAP AND JSON DIDN'T GET ON AS WELL AS I HAD HOPED....get("application-name") is not working as expected...
  17. // I THINK THIS IS BECAUSE IT'S A MULTI-DIMENSIONAL ENTITY - IF IT WAS A FLAT KEY-VALUE PAIR ARRAY, I THINK IT WOULD WORK...
  18. // Convert the Text to a more useful map...it is naturally in JSON format

  19. info "These are the JSON parsing results....";
  20. mapFromResponseText = ResponseText.toMap();
  21. info "mapFromResponseText is " + mapFromResponseText;

  22. // This line FAILS...a null is returned..

  23. theKeyValue = mapFromResponseText.get("application-name");