Search code examples
arraysjsonjolt

Jolt concatenate array into a single JSON object


I am trying to flatten the following JSON :

{
  "barcode": "7490b0d2-dfe5-4a21-bc21-02de587e2fe8",
  "containers": [
    {
      "L_ID": "867bb595-d2a4-4424-a3c3-c96cf9f4d5ee",
      "barcode": "e75d1dc6-73c7-43ea-a805-27d27839f226"
    },
    {
      "L_ID": "737dd78f-4657-4be3-a989-c647a6f3c9b6",
      "barcode": "42e918a2-6417-4e4b-9975-c5332fba2b86"
    }
  ]
}

Using the following jolt spec:

[
  {
    "operation": "shift",
    "spec": {
      "barcode": "barcode",
      "containers": "&"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "containers": "=concat('',@(1,containers))"
    }
  }
]

and i end up with the following transformation

{
   "barcode" : "7490b0d2-dfe5-4a21-bc21-02de587e2fe8",
   "containers" : "[{L_ID=867bb595-d2a4-4424-a3c3-c96cf9f4d5ee, barcode=e75d1dc6-73c7-43ea-a805-27d27839f226}, {L_ID=737dd78f-4657-4be3-a989-c647a6f3c9b6, barcode=42e918a2-6417-4e4b-9975-c5332fba2b86}]"
}

I need the string to be a parsable json string... something like the following:

{
   "barcode" : "7490b0d2-dfe5-4a21-bc21-02de587e2fe8",
   "containers" : "[{'L_ID':'867bb595-d2a4-4424-a3c3-c96cf9f4d5ee', 'barcode':'e75d1dc6-73c7-43ea-a805-27d27839f226'}, {'L_ID':'737dd78f-4657-4be3-a989-c647a6f3c9b6', 'barcode':'42e918a2-6417-4e4b-9975-c5332fba2b86'}]"
}

How can I update my jolt to get a parsable JSON string?


Solution

  • Manipulating quotes is challenging. You'd better using shift transformation rather than a modify while adding needed single quotes. The first consecutive shift specs do this :

    [
      {
        "operation": "shift",
        "spec": {
          "barcode": "&",
          "containers": {
            "*": {
              "*": {
                "*": "&3.&2.'&1'.'&'"
              }
            }
          }
        }
      },
      {//convert back to the original JSON with quoted key-value pairs
        "operation": "shift",
        "spec": {
          "barcode": "&",
          "containers": {
            "*": {
              "*": {
                "*": {
                  "$": "&4[&3].&2"
                }
              }
            }
          }
        }
      },
      {//convert the JSON to independent strings under each nodes of the "containers" array 
        "operation": "modify-overwrite-beta",
        "spec": {
          "c*": "=toString"
        }
      },
      {//stringify whole "containers" array 
        "operation": "modify-overwrite-beta",
        "spec": {
          "c*": "=join(',',@(1,&))"
        }
      },
      {//replace "=" characters by ":"s within the following two specs 
        "operation": "modify-overwrite-beta",
        "spec": {
          "c*": "=split('=',@(1,&))"
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "c*": "=join(':',@(1,&))"
        }
      },
      {//add the desired square brackets
        "operation": "modify-overwrite-beta",
        "spec": {
          "c*": "=concat('[',@(1,&),']')"
        }
      }
    ]
    

    the demo on the site Jolt Transform Demo Using v0.1.1 is :

    enter image description here