1

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:

[
  {
    "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.

EDIT:

I actually missed part of the input when writing the example, but thanks to @Barbaros Özhan answer I was able to complete my transformation.

For future rference if anybody else needs it my original input had additonal fields like:

{
  "component": {
    "parameters": [
      {
        "option": "true",
        "parameter": {
          "name": "var_name",
          "description": "desc",
          "value": "val"
        }
      },
      {
        "option": "false",
        "parameter": {
          "name": "1",
          "description": "desc",
          "value": "2"
        }
      }
    ],
    "other value": "baz"
  },
  "additional": "onemore"
}

And the final transformation I used was slightly tweaked to account for those like so:

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

1 Answer 1

1

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")
    }
  }
]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much!!! Is it a correct interpration to say that your additions (the '@' and the '*') cover the non matching cases and replicate the structure?
nice to help @Voxeldoodle , have a nice study!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.