'Jolt unnest list to top level

I have a JSON:

[
  {
    "id": 1015380,
    "type": "campaign",
    "stats": [
      {
        "message_sends_by_any_user": 13,
        "ctr": "0.094",
      }
    ]
  },
  {
    "id": 1015695,
    "type": "campaign",
    "stats": [
      {
        "message_sends_by_any_user": 7,
        "ctr": "0.091",
      }
    ]
  }
]

I want to "unnested" stats list to top level. JOLT config:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "[&1].id",
        "type": "[&1].type",
        "stats": {
          "*": "[&2].&"
        }
      }
    }
  }
]

Expected:

[
  {
    "id": 1015380,
    "type": "campaign",
    "message_sends_by_any_user": 13,
    "ctr": "0.094",
  },
  {
    "id": 1015695,
    "type": "campaign",
    "message_sends_by_any_user": 7,
    "ctr": "0.091"
  }
]

But actual output contains array indexes like this:

[ 
 {
   "id" : 1015380,
   "type" : "campaign",
   "0" : {
           "message_sends_by_any_user" : 13,
           "ctr" : "0.094"
         }, ...

How to avoid these indexes?



Solution 1:[1]

You can prefer walking through the indexes of the stats list by taking id and type attributes inside such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "stats": {
          "*": {
            "@(2,id)": "[&3].id",
            "@(2,type)": "[&3].type",
            "*": "[&3].&"
          }
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com is

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barbaros Özhan