External API array data output

Hi,

I’m having trouble with the nested array output from the data structure of this External API. I’m unable to retrieve all the values from the nested array ‘OptionalDocuments’. I will need to replace those values with the real doc titles by using TWIG after retrieving them. Whatever I tried led me to an empty output or the word “array” instead.

Hello @referee,

Thank you for the inquiry.

Have you tried extracting the value from the OptionalDocuments array using the Looper Provider > Dynamic Content? You can also directly access it using Twig by iterating through the PersonTypes array and then creating another loop for OptionalDocuments.

Please provide the login details in the secure note so that we can check it properly.

Best regards.

Hi @Ismael,
No, I haven’t. I have just added a Secure Note to the initial request. Here is a screenshot where I tested that case out. The Initial Global Endpoint is configured within the Main Component.

Thank you for the info. The page looks different when we edited it compared to your screenshot.

Is it the correct page?

@Ismael, you need to scroll below. There are two blocks that have conditions. What you see in the screenshot is the second block, which is located lower. Activate Ignore Element Conditions.

Hello John,

Since the {{ looper.item.OptionalDocuments }}, you need to nest another Looper Provider Array to be able to display the contents of that array.

Hope this makes sense.

@Ismael,

It’s there. Should I do that some other way to make it work in this case? The main difference from other cases I have no issues with is that this array contains values only. We have nested arrays containing various data types that allow us to retrieve specific values. But in this case, it just the array name and the values inside [ ] with no data types. Maybe it should be called in a different way?

I even tried to make a div with “PersonTypes” as a looper provider and inside of it another div with “OptionalDocuments” as a looper provider with a nested header element that outputs the current item with {{ looper.item }} but no success.

The API also returns a “DocumentTypes” array at the root level, besides the nested one under “PersonTypes > OptionalDocuments.” I tried to output its values, but also without success.

Thank you for the info.

We checked the contents of the first item in the PersonTypes array, and as shown in the screenshot below, “OptionalDocuments” is empty. We also noticed that the “TypeID” is different from the one in your screenshot, so it’s possible that this is from a different source or item.

Please check the parent Looper or the source and make sure that it retrieves the correct item.

Best regards.

@Ismael,

Thank you for pointing this out. That was due to a wrong fallback in the Global API endpoint. Now I can get the array data items. :ok_hand:

The final step is to replace the numerical references with specific document titles using TWIG and adding a condition to prevent displaying the docs if the array is empty. I will try to figure it out myself and come back again if I am not able to solve it.

One additional question, however.

How can I add a comma as a separator for document titles in TWIG, but only if the item is not the last one in the array?

I need to rewrite my code properly with this in mind.

{% if looper.item == 3000 %}
Паспорт родителя{% if not loop.last %},{% endif %}
{% elseif looper.item == 500 %}
Свидетельство о рождении{% if not loop.last %},{% endif %}
{% elseif looper.item == 50 %}
Фото{% if not loop.last %},{% endif %}
{% elseif looper.item == 100 %}
Паспорт{% if not loop.last %},{% endif %}
{% elseif looper.item == 105 %}
Паспорт с пропиской{% if not loop.last %},{% endif %}
{% elseif looper.item == 200 %}
СНИЛС{% if not loop.last %},{% endif %}
{% elseif looper.item == 250 %}
ИНН{% if not loop.last %},{% endif %}
{% elseif looper.item == 480 %}
Удостоверение многодетной семьи{% if not loop.last %},{% endif %}
{% elseif looper.item == 1000 %}
Сертификат RUSADA{% if not loop.last %},{% endif %}
{% elseif looper.item == 1100 %}
Банковские реквизиты{% if not loop.last %},{% endif %}
{% elseif looper.item == 1975 %}
Справка об отсутствии судимостей{% if not loop.last %},{% endif %}
{% elseif looper.item == 30100 %}
Медицинский допуск{% if not loop.last %},{% endif %}
{% elseif looper.item == 30300 %}
Спортивная страховка{% if not loop.last %},{% endif %}
{% else %}
Другое
{% endif %}

The expression {% if not loop.last %},{% endif %} will only work inside a for loop.

Example:

{% for location in locations %}
   {{ location }}{% if not loop.last %}, {% endif %}
{% endfor %}

Are you iterating through an array? If not, try adding the comma next to the title or name, without the endif condition.

The thing is that I also need to replace the figure values with word values via TWIG. I can’t use {{ location }} to output values because, let’s say I need to show “New York” instead of its digital code from the array. And on top of that, I would like to use a separator if that referenced value was not last.

When using New York and the comma after the city’s title, we will always have it there, even if it’s the last item in the array.

We can skip separators, of course, but still it would be nice to have them.

let’s say I need to show “New York” instead of its digital code from the array. And on top of that, I would like to use a separator if that referenced value was not last.

You can try something like this, assuming you’re looping through an array:

{% set documents = THE_ARRAY_HERE %}
{% for document in documents %}
   {% if document == 3000 %}
   Паспорт родителя
   {% elseif document == 500 %}
   Свидетельство о рождении
   {% elseif document == 50 %}
   Фото
   {% endif %}
   {% if not loop.last %}, {% endif %}
{% endfor %}

Let us know the result.

@Ismael,

Sorry to bother again, but how should I properly state the array in the first string?

The output of values for the element is ‘looper.item’ at the moment. But if I put it as is in the first string inside [ ], it returns the substituted words for digital codes but no separators still.

{% set documents = [looper.item] %}
{% for document in documents %}
   {% if document == 50 %}
   фото
   {% elseif document == 500 %}
   свидетельство о рождении
   {% elseif document == 100 %}
   паспорт
   {% elseif document == 105 %}
   паспорт с пропиской
   {% elseif document == 3000 %}
   паспорт родителя
   {% elseif document == 200 %}
   СНИЛС
   {% elseif document == 250 %}
   ИНН
   {% elseif document == 480 %}
   удостоверение многодетной семьи
   {% elseif document == 1000 %}
   сертификат RUSADA
   {% elseif document == 1100 %}
   банковские реквизиты
   {% elseif document == 1975 %}
   справка об отсутствии судимостей
   {% elseif document == 30100 %}
   медицинский допуск
   {% elseif document == 30300 %}
   спортивная страховка
   {% else %}
   другое
   {% endif %}
   {% if not loop.last %}, {% endif %}
{% endfor %}

We duplicated the element and directly accessed PersonTypes.0.OptionalDocuments in the Twig template instead of using a Looper Provider > Dynamic Content.

Thank you @Ismael for helping out with that. :handshake:

I really appreciate your patience in getting it done.

You are most welcome, John. We’re glad @Ismael was able to help you out.