# JSONata: From Array of objects to array of array with appending data

**URL:** <https://discourse.nodered.org/t/jsonata-from-array-of-objects-to-array-of-array-with-appending-data/28033>\
**Category:** General\
**Created:** [9 June 2020 08:00 UTC](https://discourse.nodered.org/t/jsonata-from-array-of-objects-to-array-of-array-with-appending-data/28033 "2020-06-09T08:00:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JSONata](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@JSONata](https://discourse.nodered.org/u/JSONata)\
**Post date:** [9 June 2020 08:00 UTC](https://discourse.nodered.org/t/jsonata-from-array-of-objects-to-array-of-array-with-appending-data/28033/1 "2020-06-09T08:00:48Z")

</div>

So there is this input array

```auto
 [
    {
      "time": 1591653600,
      "level": 100
      
    },
    {
      "time": 1591657200,
      "level": 110
      
    }    
]

```

To each array content there should be appended { "tag" : "east" }, so that we get an array of arrays and it looks like

```auto
[
  [
    {
      "time": 1591653600,
      "level": 100
    },
    {
      "tag": "east"
    }
  ],
  [
    {
      "time": 1591657200,
      "level": 110
    },
    {
      "tag": "east"
    }
  ]
]

```

Since the real object contains much more key/value pairs than time and level I need a general solution.

I've tried

```auto
[   
    [ 
        $,
    {
        "tag": "east"
    }
    ]
]

```

But this adds the tag only once in the end and doesn't create a real array of arrays.

You can try at [https://try.jsonata.org/Dfz2Antml](https://try.jsonata.org/Dfz2Antml)

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [9 June 2020 12:29 UTC](https://discourse.nodered.org/t/jsonata-from-array-of-objects-to-array-of-array-with-appending-data/28033/2 "2020-06-09T12:29:37Z")

</div>

Try this....

```auto
$map(payload, function($v) {
    [
        $v,
        { "tag": "east" }
    ]
})

```

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [9 June 2020 13:02 UTC](https://discourse.nodered.org/t/jsonata-from-array-of-objects-to-array-of-array-with-appending-data/28033/3 "2020-06-09T13:02:09Z")

</div>

I think the following jsonata query does the job:

```auto
$.[
     $,
     {
      "tag": "east"
     }
   ]

```

---

<div class="post-metadata">

**Author:** ![JSONata](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@JSONata](https://discourse.nodered.org/u/JSONata)\
**Post date:** [9 June 2020 13:27 UTC](https://discourse.nodered.org/t/jsonata-from-array-of-objects-to-array-of-array-with-appending-data/28033/4 "2020-06-09T13:27:35Z")

</div>

> [@michaelblight](#):
>
> ```auto
> $map(payload, function($v) {
> [
> $v,
> { "tag": "east" }
> ]
> })
> 
> ```

Thanks. Both provided solutions work. Here in this case it is "$" instead of payload, but in NodeRED than payload is the right term

```auto
$map($, function($v) {
    [
        $v,
        { "tag": "east" }
    ]
})

```

For the interested: I like understanding what I'm doing so I've looked it up. The second solution, which I marked as solution is basically the same as short term for "$map" and the function as filter

> **[Path Operators · JSONata](https://docs.jsonata.org/path-operators)**
>
> The path operators underpin the declarative nature of the map/filter/reduce processing model in JSONata.

---

<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:** [23 June 2020 13:27 UTC](https://discourse.nodered.org/t/jsonata-from-array-of-objects-to-array-of-array-with-appending-data/28033/5 "2020-06-23T13:27:35Z")

</div>

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