0

Convert XML API call response into JSON object.

Hi, after a successful call to a public API, the response that I get is of the following form:

{"result":"<Page><timestamp>2023-12-11T01:37:06.405</timestamp><contents><item><id>10381</id><code>N18</code><title>Χρόνια νεφρική ανεπάρκεια</title><description>Χρόνια νεφρική ανεπάρκεια</description><notUpdPharCost>false</notUpdPharCost><onlyByProtocol>false</onlyByProtocol><updateDate>2023-09-25 15:14:32</updateDate><insertDate>2000-01-01 00:00:00</insertDate><startDate>2023-01-01 00:00:00</startDate><endDate>2023-09-25 23:59:59</endDate><onlyByProtocolAgeLimit/></item><item><id>4929</id><code>N18.8</code><title>Άλλη χρόνια νεφρική ανεπάρκεια</title><description>Ουραιμική:\n νεφροπάθεια+ (G63.8*)\nπερικαρδίτιδα+ (I32.8*)\n\n\n\n\n\n\n\n\n\n\n\n\n\n</description><notUpdPharCost>false</notUpdPharCost><onlyByProtocol>false</onlyByProtocol><updateDate>2023-09-25 15:14:32</updateDate><insertDate>2000-01-01 00:00:00</insertDate><startDate>2023-01-01 00:00:00</startDate><endDate>2023-09-25 23:59:59</endDate><onlyByProtocolAgeLimit/></item><item><id>4930</id><code>N18.9</code><title>Χρόνια νεφρική ανεπάρκεια, μη καθορισμένη</title><description>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n</description><notUpdPharCost>false</notUpdPharCost><onlyByProtocol>false</onlyByProtocol><updateDate>2023-09-25 15:14:32</updateDate><insertDate>2000-01-01 00:00:00</insertDate><startDate>2023-01-01 00:00:00</startDate><endDate>2023-09-25 23:59:59</endDate><onlyByProtocolAgeLimit/></item></contents><count>3</count><totalEntries>3</totalEntries><totalPages>1</totalPages><firstPage>true</firstPage><lastPage>true</lastPage></Page>"}

Please let me know if I'm wrong, but the response result is in XML format. How could I convert it to a JSON object to access the individual parameters of the response body?

I have already tried to use the parseXML() function without success 😞.

2 replies

null
    • Ninox developer
    • Fabio
    • 4 mths ago
    • Reported - view

    Hi

    Yes, parseXML() formula is the one to use. Test without the "result" label:

    let myXML := "<Page><timestamp>2023-12-11T01:37:06.405</timestamp><contents><item><id>10381</id><code>N18</code><title>Χρόνια νεφρική ανεπάρκεια</title><description>Χρόνια νεφρική ανεπάρκεια</description><notUpdPharCost>false</notUpdPharCost><onlyByProtocol>false</onlyByProtocol><updateDate>2023-09-25 15:14:32</updateDate><insertDate>2000-01-01 00:00:00</insertDate><startDate>2023-01-01 00:00:00</startDate><endDate>2023-09-25 23:59:59</endDate><onlyByProtocolAgeLimit/></item><item><id>4929</id><code>N18.8</code><title>Άλλη χρόνια νεφρική ανεπάρκεια</title><description>Ουραιμική:\n νεφροπάθεια+ (G63.8*)\nπερικαρδίτιδα+ (I32.8*)\n\n\n\n\n\n\n\n\n\n\n\n\n\n</description><notUpdPharCost>false</notUpdPharCost><onlyByProtocol>false</onlyByProtocol><updateDate>2023-09-25 15:14:32</updateDate><insertDate>2000-01-01 00:00:00</insertDate><startDate>2023-01-01 00:00:00</startDate><endDate>2023-09-25 23:59:59</endDate><onlyByProtocolAgeLimit/></item><item><id>4930</id><code>N18.9</code><title>Χρόνια νεφρική ανεπάρκεια, μη καθορισμένη</title><description>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n</description><notUpdPharCost>false</notUpdPharCost><onlyByProtocol>false</onlyByProtocol><updateDate>2023-09-25 15:14:32</updateDate><insertDate>2000-01-01 00:00:00</insertDate><startDate>2023-01-01 00:00:00</startDate><endDate>2023-09-25 23:59:59</endDate><onlyByProtocolAgeLimit/></item></contents><count>3</count><totalEntries>3</totalEntries><totalPages>1</totalPages><firstPage>true</firstPage><lastPage>true</lastPage></Page>";
    parseXML(myXML)
    

     

    Fabio

      • szormpas
      • 4 mths ago
      • Reported - view

       

      thank you, you are right!

      I finally managed to isolate the XML string and convert it using the parseXML() function to a JSON object with the following code:

      if response.error then
          'API Response' := text(response.error)
      else
          let xml := text(response.result);
          let json := parseXML(xml);
          'API Response' := text(for i in json.Page.contents.item do
                  i.title.@
              end)
      end
      

       I discovered that at least in my case only xml := text(response.result) works and not xml := formatJSON(response.result) which I had used initially. I can't confidently explain why the latter is wrong!

Content aside

  • Status Answered
  • 4 mths agoLast active
  • 2Replies
  • 74Views
  • 2 Following