Jolt Transformation to flatten multi-value key input

Viewed 27

I'm trying to flatten a JSON output using recursion but I'm not getting the desired output and I'm not sure how to go about it.

Here's an example of what I'm trying to achieve.

Input JSON:

{
  "foo": [
    {
      "case": "case1",
      "doc": [
        {
          "name": "name1",
          "id": "id1"
        },
        {
          "name": "name2",
          "id": "id2"
        }
      ]
    },
    {
      "case": "case2",
      "doc": [
        {
          "name": "name3",
          "id": "id3"
        },
        {
          "name": "name4",
          "id": "id4"
        }
      ]
    }
  ]
}

Jolt Spec I'm currently using:

[
  {
    "operation": "shift",
    "spec": {
      "foo": {
        "*": {
          "doc": {
            "*": {
              "name": "Doc[&1].name",
              "id": "Doc[&1].id",
              "@(2,case)": "Doc[&1].case"
            }
          }
        }
      }
    }
  }
]

Output Received:

{
  "Doc": [
    {
      "case": [
        "case1",
        "case2"
      ],
      "name": [
        "name1",
        "name3"
      ],
      "id": [
        "id1",
        "id3"
      ]
    },
    {
      "case": [
        "case1",
        "case2"
      ],
      "name": [
        "name2",
        "name4"
      ],
      "id": [
        "id2",
        "id4"
      ]
    }
  ]
}

Output Required:

{
  "Doc": [
    {
      "case": "case1",
      "name": "name1",
      "id": "id1"
    },
    {
      "case": "case2",
      "name": "name2",
      "id": "id2"
    },
    {
      "case": "case3",
      "name": "name3",
      "id": "id3"
    },
    {
      "case": "case4",
      "name": "name4",
      "id": "id4"
    }
  ]
}

I rummaged through a bunch of docs but couldn't find anything helpful.

1 Answers

Looks like you need to add one more node to distinguish the sub-objects. Btw, no need to to replicate each attribute name individually, rather use symbols * and & such as

[
  {
    "operation": "shift",
    "spec": {
      "foo": {
        "*": {
          "doc": {
            "*": {
              "@(2,case)": "[&3].&1.case",
              "*": "[&3].&1.&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "Doc"
      }
    }
  }
]

enter image description here

Related