# Data Structures in n8n by Harshil Agrawal

## Метаданные

- **Канал:** n8n
- **YouTube:** https://www.youtube.com/watch?v=mQHT3Unn4tY
- **Дата:** 23.07.2021
- **Длительность:** 11:50
- **Просмотры:** 16,064
- **Источник:** https://ekstraktznaniy.ru/video/15786

## Описание

In this talk, Harshil Agrawal explains the data structure in n8n.

## Транскрипт

### <Untitled Chapter 1> []

all right uh so today i'm going to talk about data structures in n10 i know this is a bit more technical topic but i'm going to try my best to explain and go through a bit slowly and try to cover everything so that you understand how the data structure in anything works how what when you might need to you know use of the function node to transform data from one structure to another so let's get started and take uh an overview of how it looks so this is whenever you make a request why http uh by the http request node and you are getting some data from another api this is the top level structure that looks uh that you will get so let's just break it down into small parts so first of all if you see there is a arranged box that is called items so that's uh that's the whole array that max showed us earlier right and each and in this particular items box we have smaller boxes which are each individual item right i am going to go through an example to show you the difference over here but yeah but moving forward now each of this item can have a box within itself which can which contains the value json and now each json can have another box within itself which contains different values so these values can be your actual data so something like you know the order id the name of the particular order the price and everything like that so moving forward so this is how the overall uh your data might look like so again there is a box which is an items and each of this box contains uh different boxes which have which which we call as json objects and then in this json objects there are another boxes which actually contain your data so over here you can see that our actual data is the name the age and the rule so this is how the overall structure looks like in ntn now moving forward over here i this is a screenshot of one of the nodes so if you execute the node you get a list of items in here now if you see we have on the nodes we specify the total number of items that get returned and each of this row is an individual item so you can see that the total number of items here is 5 and each of these rows is an individual item now let's take a look at how it actually looks under the hood in any time so for this i have taken an example of the function node because we often times use the function node for data transformation so the important thing to note over here is that you can see that there is the json key for each and every item in here now this is the actual code that you write but this is the value that anytime displays you so na10 does not show you the json key it only gives you the required data so sometimes i have seen a lot of users get confused from uh and this is why i wanted to specify this particular thing that we uh we don't extract the json object we don't display to you but it's under the hood this is how it looks like

### n8n DS: Full picture [3:21]

so again coming back to and taking a full picture over here we have an items box which contains individual smaller boxes which are json objects and this json objects they contain your actual data so this data can have you know any kind of structure it can be another object or it can be an array so now let's take a look at it again i'm gonna steal my access example over here and i'm gonna go through the same example so i'm gonna execute the node and it is gonna return the same data that max showed us but over here if you see it says only one item but we have a lot of different orders in here now this is because if i close this we have the main array so the main box which was in the orange box and then this is the and there is this one single box in here so the orange box and there is just one box and that is why for any 10 this is only one item but if we split it into items and if i execute this now what it will do is it will now get those items from that from the inner item box and then give us the json body so if you see over here now we have the items as 30 and now we have each of these individual items so this was the items and why you should care about it so one of the reasons is often times we get all these values and we want to you know do some process on each of these values but if you don't have the values coming in as this uh as the intended data structure only the first item in that value gets processed and you then you know have to you maybe loop around it and figure out a way of how you can process this uh all the items so if you convert your incoming request into the entertain data structure you don't have to create any sort of loop so anytime then handles it for you so let's take a quick look over here so i'm just gonna use the set node for now and i'm gonna set a value over here let's just get the employee name right now another important thing to note over here is we just show the first value over here but we are going to process all the incoming data so if i execute this node you see that we have we are getting the value for all the incoming data and not only just the first item now this is again because we are following the ntn data structure but what happens when we don't do that so let's turn this off execute the node now remember we are now just getting only one item so i have to go ahead and change the expression in here so you see now that like each of this becomes an array and we are only able to specify one particular value so now if i execute this node it only gives me the first value so this is where you really uh this is where uh transforming your data into the uh into the data structure that and it can follow is really helpful when you uh when you want to process all the information for each of this incoming items now since we are talking about transformation let's take a look at how you can do it

### DS Transformation: Access data [7:00]

so uh to access the data there we since anytime uses javascript under the hood we are using javascript uh you yeah we using javascript notations to access the data so again items is the whole box so we are telling hey we need something from the items okay cool but what do you need now so we need the first value from the items so if you are familiar with uh arrays in javascript the first value always is indexed with zero so because i want the first object i am telling uh from items and then what exactly do i want so i want this particular json object and that's why i then specified authentication so what this will return is this will return the whole uh json object which is the name the age and the rule but if i want to uh let's just say get the second item right so over then i have to specify items and then change the index value over here and then i can calculation what happens when we want to you know get a specific value let's see that's the name so in that case what we do is we just you know after json we again use a dot notation so this becomes dot json dot name so now it will only written as the name and it won't give us the age and the role

### DS Transformation: Ex. 2 [8:22]

so now let's take a look at how it looks like at that from a third-party api and how it looks like in any time so whenever you make a request uh on the third party or for the third party api uh maybe using a tool like postman right you might get the data in this format but for anytime this is how it looks under the hood right and if you see over here we have the results array and we want to just get the results right we just don't we don't want the count or the errors we want the actual results so that we can process it further so for that what we do is we use the uh map function of javascript to specif and extract those values so what will happen over here is it will go through the results array maps to those values and give you only this particular object and then you can connect another node and then process all that information moving forward to the next example sometimes you might want to combine all these qualities together and this is where this example is really useful to understand so let's just say we now have different values in here right we have a json object which contains name john a name mary and now we want to combine them and get it into one single object json object so that's why this kind of code snippet is really helpful so what it does is it goes through all these items and it gets those values and it then uses the join operation to joins them so this is how it would look like so in the end my message would be uh john mary and someone whose name starts with p right so something like that so this was a small overview of uh data structuring and time if you have any questions i would love to address them so feel free to post your questions in the chat and i just to take a quick summary of what we

### Summary [10:25]

talked about so we talked about that if your incoming data is following the data structure of na10 your data will travel from node to node and it's called items and inside the list each item contains an object we saw the inner box that has the json and inside the object again it is the key json and sometimes if you are working with files the key might change from json to binary and if you want to transform your data there are essentially three steps that you should take care of is to first recognize which is the incoming data structure so is it just an array within an area is it just uh is it a an array that contains all those json items the next is to extract with you know with the expressions so something like item0. json and then that particular value and the last is to return it into the compliant data structure using the javascript methods that i talked about earlier we already have a code snippet documented which contains all the most common code snippets that you might have that you might need so you can always head over to our documentation and it's in under the reference section and again if you have if you're facing any kind of problem or if you have more questions around transforming your data you can always ask us the questions on our community forum
