Search code examples
jsonata

Jsonata, merging array of objects


I have an array of objects that I would like to reformat using a jsonata expression

{
    "items": [
        {
            "time": 1575417919282,
            "message": {
                "data": 21,
                "type": "temperature"
            }

        },
        {
            "time": 1575417919282,
            "message": {
                "data": 45,
                "type": "temperature"
            }

        }
    ]
}

Desired format

[
    {
        "data": 21,
        "type": "temperature",
        "time": 1575417919282
    },
    {
        "data": 45,
        "type": "temperature"
        "time": 1575417919282
    }
]

Is there an easy one liner for this? I started with merging time into the message object using $merge([$.items.message, {"time":$.items.time}]) but his gives me

{
  "data": 45,
  "type": "temperature",
  "time": [
    1575417919282,
    1575417919282
  ]
}

I'm finding the documentation hard to follow. How do you start with just merging two objects iteratively?


Solution

  • This will do it:

    items.{
       "data": message.data,
       "type": message.type,
       "time": time
    }
    

    http://try.jsonata.org/SJZDsyHTr