skip to Main Content

I’m newbie in jolt and I trying to transform the following JSON

Headers can be different in different json.

I really need a spec based on special characters like *,@,&,$

I’ll be eternally grateful for your help!

Input JSON :

[
  {
    "header1": null,
    "header2": null,
    "header3": null
  },
  {
    "value1": null,
    "value2": null,
    "value3": null
  },
  {
    "value4": null,
    "value5": null,
    "value6": null
  }
]

Expected Output :

[
  {
    "header1": "value1",
    "header2": "value2",
    "header3": "value3"
  },
  {
    "header1": "value4",
    "header2": "value5",
    "header3": "value6"
  }
]

2

Answers


  1. You can use the following shift transformation spec

    [
      {// separate the first object which has the headers
        "operation": "shift",
        "spec": {
          "0": {
            "*": {
              "$": "h"
            }
          },
          "*": {
            "*": {
              "$": "v&2"
            }
          }
        }
      },
      {// match "headers" against "values" while reforming new nodes to hold each key-value pair group within different objects
        "operation": "shift",
        "spec": {
          "v*": {
            "*": {
              "*": {
                "@1": "&3.&2.@(4,h[&2])"
              }
            }
          }
        }
      },
      {// get rid of object keys
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "[#3].&"
            }
          }
        }
      }
    ]
    

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

    enter image description here

    Login or Signup to reply.
  2. You can solve this problem with 2 shift operations like the below:

    [
      {
        "operation": "shift",
        "spec": {
          "0": {
            "*": {
              "$": "header"
            }
          },
          "*": "data"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "data": {
            "*": {
              "*": {
                "$": "[&2].@(4,header[#2])"
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search