Search code examples
jsonjolt

How to list the keys available in a object in jolt


I am trying to add all the keys available in input to the object named original and these are intialised to {}.

Input:

{
  "PartyStatus": {
    "item": [
      {
        "RleStsVal": {
          "prtyRleStsVal": "0001",
          "prtyRleStsTyp": "Active"
        },
        "PrtyRleStgCd": {
          "PrtyRleStgTp": "0002",
          "PrtyRleStgDesc": "Prospect"
        }
      }
    ]
  }
}

Expected Output:

{
  "PartyStatus": {
    "item": [
      {
        "RleStsVal": {
          "prtyRleStsVal": "0001",
          "prtyRleStsTyp": "Active"
        },
        "RleStgCd": {
          "PrtyRleStgTp": "0002",
          "PrtyRleStgDesc": "Prospect"
        },
        "$original": {
          "RleStgCd": {},
          "RleStsVal": {}
        }
      }
    ]
  }
}

in the output, $original object should have all the keys of the objects available initialised with with empty braces


Solution

  • You can use the following transformation :

    [
      {//generate an object with key "$original"
        "operation": "shift",
        "spec": {
          "PartyStatus": {
            "item": {
              "*": {
                "*": {
                  "*": "&4.&3[&2].&1.&",
                  "$": "&4.&3[&2].\\$original.&" //$ character shold be escaped, since it's a reserved one as a wildcard
                }
              }
            }
          }
        }
      },
      {//overwrite the values of the newly generated atributes by {} respectivly
        "operation": "modify-overwrite-beta",
        "spec": {
          "PartyStatus": {
            "item": {
              "*": {
                "\\$original": {
                  "*": {} 
                }
              }
            }
          }
        }
      }
    ]
    

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

    enter image description here