[Solved] How can I access the property of the json retrieved from an API?


I have integrated my API properly and it returns a json like this:


{

“userId”: 1,

“id”: 1,

“title”: “sunt aut facere repellat provident occaecati excepturi optio reprehenderit”,

“body”: “quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto”

}


For example, the variable name of that is test, so in the conversation, i will have to access it by using {{test}}. How can I access a property of this json, for example, i only want to access the title.


Note: {{test.title}} does not work.


9 comentários

When your API return a JSON, you must to set a name for your variable. That name will be used to access your JSON’s properties. In your case, you’ve set “test”. To access your propeties, always use that sintax below:


{{test@title}}, who “test” is the name of the variable you setted from your API return and “title” is the property from your JSON.


Obs: sorry for my bad English hahaha.

Thank you very much! That worked. =)

I have another question, what if I want to access a specific array value that is in the json?

BLiP plataform cannot work with arrays yet. If you want to access a specific value, you’ll need to create a Script (JS) and work with your JSON there. For example, imagine that you receive this JSON from your API:


{
"title" : "Fruits",
"values" : [
"Apple",
"Banana",
"Orange"
]
}

Your script will be something like this:


function run(responseAPI) {

responseAPI = JSON.parse(responseAPI);
let fruits = responseAPI.values;
return {
example1 : fruit[0],
example2 : fruit[1],
example3 : fruit[2]
};
}

You can return an object from your Script (JS). But, for returning your array, you’ll need to specify the index that you want to access.

Okay. Thank you very much 🙂

Hi! I was wondering where in here will I be able to put the javascript code?


You have an action called “Execute script” (i dont know if that’s called like this in English).



When you create that, you can write javascript code in this option:


Oh I see. Thank you so much Leonardo! =)

Thank you very much @Leonardo_Lopes_Silva for all your contributions 😉

Comente