Combine two values to an object with a JOLT transformation

Viewed 36

I am pretty new to Jolt. The source format was generated from some software and does not have a good structure, so I would like to transform this. Each text ("First text" and "Second text") has an associated link ("First text" -> "Link of first text"; "Second text" -> "Link of second text"). How would you do such a transformation? Any help please?

Sample Input:

{
  "flow": [
    "^First text",
    {
      "*": ".^.c-0"
    },
    "^Second text",
    {
      "*": ".^.c-1"
    },
    {
      "c-0": [
        {
          "->": "Link of first text"
        }
      ],
      "c-1": [
        {
          "->": "Link of second text"
        }
      ]
    }
  ]
}

Expected Output:

{
  "flow": {
    "links": [
      {
        "message": "^First text",
        "link": "Link of first text"
       },
      {
        "message": "^Second text",
        "link": "Link of second text"
       }
     ]
  }
}
1 Answers

What you need is to use shift transformation.

One option is to create individual arrays(lists) within the first, and then dissipate them to separate objects within the second transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "flow": {
        "*": {
          "*text": { // filter out by the key names ending with "text"
            "$": "&3.message" // go 3 levels up to grab the key name "flow"
          },
          "c*": { // filter out by the key names starting with "c"
            "*": {
              "*": "&4.link" // go 4 levels up to grab the key name "flow"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": { // stands for "flow"
        "*": {
          "*": "&2.links[&].&1" // &2 -> go 2 levels up to grab the key name "flow"
                                // &1 -> go 1 level up to grab the key names of the arrays
        }
      }
    }
  }
]

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

enter image description here

Related