JOLT add object name into body as new property

14 Views Asked by At

I have a JSON payload and I would like to add the software architecture designation (x86 or x64) as a new property called architecture.

{
  "name": "T15P-GEN3",
  "software": {
    "x86": [
      {
        "name": "AAA",
        "version": "1.3.4",
        "uninstall": "MsiExec.exe /I{3A969900-CAD5-4FB8-B5D1-1F8D9028C6AA}"
      },
      {
        "name": "BBB",
        "version": "1.1.2",
        "uninstall": "MsiExec.exe /I{102C3B71-22B5-4484-8D1F-1C859CA6EE63}"
      },
      {
        "name": "CCC",
        "version": "1.1.4",
        "uninstall": "MsiExec.exe /I{D65355E4-021A-4B0C-B9A4-9D90F8C0D106}"
      }
    ],
    "x64": [
      {
        "name": "AAAA (x64)",
        "version": "24.01",
        "uninstall": "'C:\\Program Files\\AAAA\\Uninstall.exe'"
      },
      {
        "name": "BBBB (2023)",
        "version": "5.5.5.2811",
        "uninstall": "MsiExec.exe /I{92EC01FE-5C8D-4D03-B8D1-F0A2AEF14EB3}"
      },
      {
        "name": "CCCC",
        "version": "",
        "uninstall": "C:\\Program Files\\CCCC\\CCCC_x64.exe -?uninstall"
      }
    ]
  }
}

Desired output would be:

{
  "name": "T15P-GEN3",
  "software": {
    "x86": [
      {
        "name": "AAA",
        "version": "1.3.4",
        "uninstall": "MsiExec.exe /I{3A969900-CAD5-4FB8-B5D1-1F8D9028C6AA}",
        "architecture": "x86"
      },
      {
        "name": "BBB",
        "version": "1.1.2",
        "uninstall": "MsiExec.exe /I{102C3B71-22B5-4484-8D1F-1C859CA6EE63}",
        "architecture": "x86"
      },
      {
        "name": "CCC",
        "version": "1.1.4",
        "uninstall": "MsiExec.exe /I{D65355E4-021A-4B0C-B9A4-9D90F8C0D106}",
        "architecture": "x86"
      }
    ],
    "x64": [
      {
        "name": "AAAA (x64)",
        "version": "24.01",
        "uninstall": "'C:\\Program Files\\AAAA\\Uninstall.exe'",
        "architecture": "x64"
      },
      {
        "name": "BBBB (2023)",
        "version": "5.5.5.2811",
        "uninstall": "MsiExec.exe /I{92EC01FE-5C8D-4D03-B8D1-F0A2AEF14EB3}",
        "architecture": "x64"
      },
      {
        "name": "CCCC",
        "version": "",
        "uninstall": "C:\\Program Files\\CCCC\\CCCC_x64.exe -?uninstall",
        "architecture": "x64"
      }
    ]
  }
}

I have tried experimenting based on the following posts JOLT add object name into body and JOLT add property based on name of another property but cannot get it to work the way I want it.

{
  "operation": "shift",
  "spec": {
    "software": {
      "*": {
        "*": {
          "@": "&3[].&",
          "$": "&3[].&.architecture"
        }
      }
    }
  }
}

I have just started working with Jolt transformations, but this one is a bit too difficult for me to solve.

1

There are 1 best solutions below

1
Barbaros Özhan On BEST ANSWER

You can use

[
  {
    "operation": "shift",
    "spec": {
      "*":"&", // the elements other than the software object
      "software": {
        "*": {
          "*": {
            "@": "&3.&2[&1]",
            "$1": "&3.&2[&1].architecture"
          }
        }
      }
    }
  }
]

where

  • $ copies from one upper level while $1 from two upper
  • &1 within [&1] loops the indexes of the indexes of the arrays(eg. x86 and x64)
  • &3 and &2 represent the keys software and x86, x64 respectively