# How to create a json using loop

**URL:** <https://discourse.nodered.org/t/how-to-create-a-json-using-loop/29390>\
**Category:** General\
**Created:** [2 July 2020 15:29 UTC](https://discourse.nodered.org/t/how-to-create-a-json-using-loop/29390 "2020-07-02T15:29:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![apex](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@apex](https://discourse.nodered.org/u/apex)\
**Post date:** [2 July 2020 15:29 UTC](https://discourse.nodered.org/t/how-to-create-a-json-using-loop/29390/1 "2020-07-02T15:29:24Z")

</div>

hi i am new to js,  
i have an json loop like:

```auto
var sammy = {
    "device": [{
    "imsi" : msg.payload.data.devices[0].imsi,
    "rg_id" : msg.payload.data.devices[0].rg_id,
    "id" : msg.payload.data.devices[0].id 
    },
    {
    "imsi" : msg.payload.data.devices[1].imsi,
    "rg_id" : msg.payload.data.devices[1].rg_id,
    "id" : msg.payload.data.devices[1].id 
    }]
}

```

how can i use a loop to create this(Json)?  
thanks

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [2 July 2020 15:39 UTC](https://discourse.nodered.org/t/how-to-create-a-json-using-loop/29390/2 "2020-07-02T15:39:28Z")

</div>

Take a look at JSONata and a change node:

> **[JSONata Documentation · JSONata](https://docs.jsonata.org/overview)**
>
> \## Introduction

  
If you set a change node to the mode that looks like a capital J with a colon, you can write JSONata queries in it.

---

<div class="post-metadata">

**Author:** ![nlecaude](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nlecaude/32/25_2.png) [@nlecaude](https://discourse.nodered.org/u/nlecaude)\
**Post date:** [2 July 2020 15:57 UTC](https://discourse.nodered.org/t/how-to-create-a-json-using-loop/29390/3 "2020-07-02T15:57:30Z")

</div>

Not sure what the final goal is but looking at your example you could probably just do

```auto
var sammy = {
     "device": msg.payload.data.devices
}

```

Otherwise look at the map() function (it exists for both javascript and JSONata)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [2 July 2020 20:24 UTC](https://discourse.nodered.org/t/how-to-create-a-json-using-loop/29390/4 "2020-07-02T20:24:05Z")

</div>

> [@apex](#):
>
> i have an json loop like:

I don't know much JS but I think the correct term is that you have a Javascript object called sammy

---

<div class="post-metadata">

**Author:** ![tbdltee](https://avatars.discourse-cdn.com/v4/letter/t/7cd45c/32.png) [@tbdltee](https://discourse.nodered.org/u/tbdltee)\
**Post date:** [3 July 2020 01:04 UTC](https://discourse.nodered.org/t/how-to-create-a-json-using-loop/29390/5 "2020-07-03T01:04:59Z")

</div>

if yuo see sammy structure, it's `sammy = {device: [[...], [...]]}`  
so you can use .push() to create the array

```auto
var sammydata = {device:[]};
for (var i = 0; i<2; i++) {
sammydata = {imsi: ...., rg_id:...,id:...};
sammy.device.push(sammydata);
}

```

you can push sammydata into sammy.device array

---

<div class="post-metadata">

**Author:** ![apex](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@apex](https://discourse.nodered.org/u/apex)\
**Post date:** [3 July 2020 03:12 UTC](https://discourse.nodered.org/t/how-to-create-a-json-using-loop/29390/6 "2020-07-03T03:12:20Z")

</div>

hi thanks for help.  
i will make my question clear  
i have some sensor working ....  
and i am working on the API...  
to get the info.  
sorry since my flow is too large to post , so i post only some info here:

Here is the info get from the API

 ![123](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/cea8c27669588a065b2135779dc8ef21bbdde4e2.png)  
1)  
since i dont know how many device exist .  
so i need a loop(?) to get the grab the data..  
which is the imsi , rg\_id  
2) and i need to get the data to form another JSON object so that to pass to the DB.

thanks if anyone can help  
thanks

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 July 2020 08:26 UTC](https://discourse.nodered.org/t/how-to-create-a-json-using-loop/29390/7 "2020-07-03T08:26:08Z")

</div>

Start with a Change node to Move msg.payload.data/devices to msg.payload (assuming the object you showed is in msg.payload, it is off the top of the screen so not certain). Feed that into a debug node to make sure the devices array is now in msg.payload. When that works feed it into a Split node, which will split the array into messages, one device record per message. Again use a debug node to check you now see one message for each element in the array. Then you can do what you want with those messages.  
If you have not already done so then read the node red docs page Working with Messages which has a lot of useful information.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 September 2020 08:26 UTC](https://discourse.nodered.org/t/how-to-create-a-json-using-loop/29390/8 "2020-09-01T08:26:08Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
