Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JOLT Keep structure after array shift

I’m trying to construct a JOLT transformation such that it will change a parameter value if the paramter name matches in a given array.
Example Input:

{
  "component": {
    "parameters": [
      {
        "parameter": {
          "name": "var_name",
          "value": "val"
        }
      },
      {
        "parameter": {
          "name": "1",
          "value": "2"
        }
      }
    ]
  },
  "additional": "onemore"
}

Desired Output:

{
  "component": {
    "parameters": [
      {
        "parameter": {
          "name": "var_name",
          "value": "new_val"
        }
      },
      {
        "parameter": {
          "name": "1",
          "value": "2"
        }
      }
    ]
  },
  "additional": "onemore"
}

My Current JOLT transform:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    "operation": "shift",
    "spec": {
      "component": {
        "parameters": {
          "*": {
            "parameter": {
              "name": {
                "var_name": {
                  "#new_val": "&6.&5[&4].&3.value"
                }
              }
            }
          }
        }
      }
    }
  }
]

The problem with my JOLT transform is that it deletes the rest of the Json, whereas I’d like to mantain it unchanged if there’s no match

I tried looking for a solution, but the closest I got was this one, which allowed me to make the current transform, but I don’t understand how to fix it properly.

>Solution :

You can use this shift transformation spec

[
  {
    "operation": "shift",
    "spec": {
      "component": {
        "parameters": {
          "*": {
            "parameter": {
              "name": {
                "@": "&5.&4[&3].&2.&1", // @ matches the value taken from one level up, eg. replicating "name"
                "var_name": {
                  "#new_val": "&6.&5[&4].&3.value"
                },
                "*": {
                  "@(2,value)": "&6.&5[&4].&3.value"
                }
              }
            }
          }
        }
      },
      "*": "&" // the "else" case(the attributes other than "component")
    }
  }
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading