Forum Academy Marketplace Showcase Pricing Features

API response parse from XML to JSON

Hi team,

I am using an API which returns info in XML by default. One of the fields returned (well, actually several) are lists of text. I have these setup as list of text as the thing type and everything works well when the list has two or more items like this:

<ListName>
    <listEntry>ABC</listEntry>
    <listEntry>BCD</listEntry>
 </ListName>

It parses to JSON as:

 "ListName": {
                    "ListEntry": [
                        "ABC",
                        "BCD"
                    ]
                }

…however when there’s only one item in the list returned by the API. The XML of this is still marked up as a nested list i.e.

 <ListName>
   <listEntry>ABC</listEntry>
</ListName>

But the parsed JSON now just shows it as a single text item and this is no longer able to be pulled in my Bubble app - i.e. the JSON looks like this:

 "ListName": {
                    "ListEntry": "ABC"
                }

What I want to happen is to still show as an array but with only one item in it. Is this possible?

1 Like

Hey,
I had the same issue in the past.

Solutions:

  • server-side plugin - is a good one
  • receive XML as text and send it your Bubble API endpoint whicg will make find and replace and send it back to you - isn’t a good one
2 Likes

I ended up setting up the API twice - once with a list and once with a single text item. When the call looking for a list finds nothing, it checks the single text item version. Bit messy but it does the job and I can’t play with the vendor API.

4 Likes

How are you getting the XML to be parsed to JSON?

1 Like

I’m now looking for a similar solution. I love the answer you got.

1 Like

The same problem for me! When one element and when many elements!!!

the same for me!!! i will try this one, but it is so crazy.