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


#1

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.


#2

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.


#3

Thank you very much! That worked. =)


#4

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


#5

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.


#6

Okay. Thank you very much :slight_smile:


#7

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


#8

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:


#9

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


#10

Thank you very much @Leonardo_Lopes_Silva for all your contributions :wink: