Jolt - Flatten JSON array

Viewed 101

Input:

{
  "creatives": [
    {
      "link_promo": [
        "https://facebook.com/123",
        "https://facebook.com/456"
      ]
    },
    {
      "link_promo": [
        "https://facebook.com/789",
        "https://facebook.com/101112"
      ]
    },
    {
      "link_promo": [
        "https://facebook.com/121314",
        "https://facebook.com/141516"
      ]
    }
  ]
}

Spec:

[
  {
    "operation": "shift",
    "spec": {
      "creatives": {
        "*": {
          "link_promo": "promo_links[]"
        }
      }
    }
  }
]

Output:

{
  "promo_links": [
    [
      "https://facebook.com/123",
      "https://facebook.com/456"
    ],
    [
      "https://facebook.com/789",
      "https://facebook.com/101112"
    ],
    [
      "https://facebook.com/121314",
      "https://facebook.com/141516"
    ]
  ]
}

Desired Output:

{
  "promo_links": [
    "https://facebook.com/123",
    "https://facebook.com/456",
    "https://facebook.com/789",
    "https://facebook.com/101112",
    "https://facebook.com/121314",
    "https://facebook.com/141516"
  ]
}

Hi guys, i need to flatten nested list to an array as description above. Hi guys, i need to flatten nested list to an array as description above. Any ideas how to modify my jolt spec to achieve that output?

1 Answers

Just nest one more step by replacing the part

"link_promo": "promo_links[]" with "link_promo": { "*": "promo_links" }

in order to reach innermost part of the JSON as in the following spec

[
  {
    "operation": "shift",
    "spec": {
      "creatives": {
        "*": {
          "link_promo": {
            "*": "promo_links"
          }
        }
      }
    }
  }
]

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

enter image description here

Related