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

So there is this input array

 [
    {
      "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

[
  [
    {
      "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

[   
    [ 
        $,
    {
        "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

Try this....

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

I think the following jsonata query does the job:

$.[
     $,
     {
      "tag": "east"
     }
   ]
1 Like

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

$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

1 Like

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