How can i Convert XML to JSON

I’m trying to grab images from Flickr API, it returns it in XML format but they also have an argument for format=json

I get the following when I submit it

jsonFlickrApi({“photos”:{“page”:1,“pages”:547568,“perpage”:1,“total”:547568,“photo”:[{“id”:“51964262546”,“owner”:“194499057@N06”,“secret”:“fb65aea0be”,“server”:“65535”,“farm”:66,“title”:“Shark In The Water”,“ispublic”:1,“isfriend”:0,“isfamily”:0}]},“stat”:“ok”})

I have it set to a single photo just for my own clarity but I cannot get this to convert to an object to grab the properties.

No matter what I try I get null.

That’s not valid JSON. It’s not formatted properly. You should be able to paste the JSON response text into Best JSON Viewer and JSON Beautifier Online and see the “tree” of values on the right side. Once you’ve got that working, post the corrected text and we can help you with the get property blocks.

Edit: sorry! Stupid smart quotes. The JSON is fine:

{"photos":{"page":1,"pages":547568,"perpage":1,"total":547568,"photo":[{"id":"51964262546","owner":"194499057@N06","secret":"fb65aea0be","server":"65535","farm":66,"title":"Shark In The Water","ispublic":1,"isfriend":0,"isfamily":0}]},"stat":"ok"}

The problem is that the “photo” property is an array/list. So you need to get the first list item of “photo” before you get the “id” property of that item.

2 Likes

Sorry I wasn’t aware of the smart quotes issue.

I used the links you provided and I see the difference.

I tried getting #1 of the list as follows but no cigar.
Am I able to upload this object to the cloud as well like this?

Not your fault. Just something the forum software does for some reason. You can avoid it by using the Preformatted text icon in the forum toolbar:

That should work. I’ll create a quick demo to test it.

You can upload just about anything to Firebase. So yes, you can upload an object to a cloud variable if that’s what you’re asking.

1 Like

I used the same blocks and it worked, displaying “51964262546”.

See the first screen here:

https://x.thunkable.com/copy/23638b6eaeaff97201b52337651a0152

So, since the JSON is valid and the blocks work, it means you need to do two things:

  1. Check the value of the green error block to see if the API server is working properly. Assign that block to a label to check it.
  2. Check the value of the green response block. Assign that block to a text input and then copy and paste the value into Best JSON Viewer and JSON Beautifier Online to see if it recognizes it as valid JSON.
1 Like

Thank you, I copied your blocks and got the same result as you.

When going back to the green response block it was null again. I found that I had to remove jsonFlickrApi and the () and then it worked as intended

Is there a way to exclude jsonFlickrApi() to avoid this in the future?

1 Like

If this is part of the response then use the substring block to get only the part you need from the response.
image

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.