How to get an object from a nested array of objects using Codeless

Application ID

6FF3B177-880D-E896-FFB6-16A8815BE100

Expected Behavior

I would like to create a variable that contains a list of the “tasks” object properties.

  1. I am successfully getting the response from an external API. This contains properties and objects.
  2. I successfully create a variable that gets a nested object from the result.
  3. I would like to create a second variable that gets a nested object from the nested object

Actual Behavior

Using the get property block, I am able to access a nested list:

becomes
image

by using the Get Property block:

Then, I want to create a variable with the contents of a nested object one level down, for example the date of a task, in green in the screenshot below:

I have tried to repeat the logic I successfully used above, but I am getting an empty result:

I suspect that there is something I still don’t undestand on how a JSON object contains lists and arrays and how these are different.

Hi @Andreas_Marinopoulos,

It is either a problem with the cropped image, or there is a problem with the JSON structure which appears invalid. Specifically, see the following:

The way the structure looks like in the screenshot, the tasks element doesn’t even belong to the document since it is invalid.

Regards,
Mark

Sorry for confusing things with the blurred out segment.

Here is how the JSON is structured. As you will notice, there is a main object has properties such as dates, links etc. But there are also arrays of objects such as addresses, tasks, custom, opportunities, contacts. If we take the example of contacts, there are nested integration link properties “name”: “Bio”, “url”: “XXX”.

My question is as follows:

What is the right logic blocks to use if I want to dig a step deeper into a JSON structure?

There is probably a different way to do it for objects and a different one for arrays of objects.
Get property of “XXX” is maybe right for objects but not for arrays of objects?

[
    {
        "addresses": [],
        "custom.lcf_q5RSAIutdMzdC3VtiLx4BaD6EBGPesTDTI2uJJXN4Qu": [
            "XXX"
        ],
        "id": "lead_nLvvTwqzsFB4gXUeaBbhD3M7T914mVWRcKjCkEpPl3j",
        "tasks": [
          {
          "date":"XXX"
           }
          ],
        "custom": {
            "*BG - State/County": "Greater Manchester",
            "*BG School Name": [
                "XXX"
            ],
            "*BG timezone": "-0h"
        },
        "opportunities": [
            {
                "note": "XXX",
                "date_created": "2020-12-14T19:37:31.618000+00:00",
                "integration_links": [
                    {
                        "name": "Send Document via PandaDoc",
                        "url": "XXX"
                    }
                ],
                "confidence": 50,
                "value_period": "one_time",
                "expected_value": 0
            }
        ],
        "created_by": "user_8ym1NFSM77aDdlFCOMjdhkPgUaX4hqfckmdkx22cWPa",
        "html_url": "XXX",
        "name": "XXX",
        "contacts": [
            {
                "date_created": "2020-12-14T19:37:26.581000+00:00",
                "integration_links": [
                    {
                        "name": "Bio",
                        "url": "XXX"
                    },
                    {
                        "name": "Video",
                        "url": "XXX"
                    },
                    {
                        "name": "Send Document via PandaDoc",
                        "url": "XXX"
                    }
                ],
                "phones": [
                    {
                        "country": "GB",
                        "type": "mobile"
                    }
                ],
                "display_name": "XXX",
                "lead_id": "lead_nLvvTwqzsFB4gXUeaBbhD3M7T914mVWRcKjCkEpPl3j"
            }
        ]
    }
]

When you get JSON back, it is treated as an object or a list. In the example above, it is a list (since everything is within [ ... ]. This means you can use the Codeless blocks which work with lists. Once you have a specific element of the list, say the first one, it is an object and you get use the get property XXX of block to retrieve property values of that object.

Does it help?

Regards,
Mark

Yes, I think so. I am still missing something though…

If I distill my confusing JSON down to the essentials, it will look like this:
[
{
“opportunities”: ,
“contacts”:
}
]

Say I have managed to get this inside a variable named enrolledParents and I now want to create a new variable for contacts.

From your comment, I understand I first need to use a list block. Looking at the blocks as well as your video on lists, I gather I first need to find the index of the contacts object in the list, right? Once I find the contacts object index, I should be able to get it.

But I am getting:
image

It is close. When you work with lists, every element in that list has an index. For example, the logic below will get the first object from the list (index 0) and then from that object will get the contacts property:
image

Regards,
Mark

Success!

At first, when I tried to return contacts or the first object from the list (index 0) like this:

I got this error:
{
“code”: 0,
“message”: “Cannot read property ‘contacts’ of undefined”
}

which seems to relate to an empty body.

My simplified JSON looks like this:
[
{
“contacts”:
}
]

I then tried getting the index 1, and it worked. This is what gave me the nested object:

With this formula, I can now go down as many layers as I need, thank you.

Hi @Andreas_Marinopoulos

Yes, there is a little difference between regular programming where a list starts from zero index and Codeless where there first element can be accessed by the first (1) index

Also, you can select first element in the list without extra “Number” block

2 Likes