skip to Main Content

I’d like to replace a value in a json with JOLT but I haven’t managed to do so.

My JSON :

{
  "MIRecord": [
    {
      "RowIndex": "0",
      "NameValue": [
        {
          "Name": "MBWHLO",
          "Value": "123"
        },
        {
          "Name": "MBITNO",
          "Value": "123"
        },
        {
          "Name": "V_NETA",
          "Value": "123"
        }
      ]
    },
    {
      "RowIndex": "1",
      "NameValue": [
        {
          "Name": "MBWHLO",
          "Value": "123"
        },
        {
          "Name": "MBITNO",
          "Value": "123"
        },
        {
          "Name": "V_NETA",
          "Value": "123"
        }
      ]
    }
  ]
}

I want to replace :

  • MBWHLO to CHANGE1
  • MBITNO to CHANGE2
  • V_NETA to CHANGE3

My target JSON :

{
  "MIRecord": [
    {
      "RowIndex": "0",
      "NameValue": [
        {
          "Name": "CHANGE1",
          "Value": "123"
        },
        {
          "Name": "CHANGE2",
          "Value": "123"
        },
        {
          "Name": "CHANGE3",
          "Value": "123"
        }
      ]
    },
    {
      "RowIndex": "1",
      "NameValue": [
        {
          "Name": "CHANGE1",
          "Value": "123"
        },
        {
          "Name": "CHANGE2",
          "Value": "123"
        },
        {
          "Name": "CHANGE13",
          "Value": "123"
        }
      ]
    }
  ]
}

I try this but i change all of "Name" value to CHANGE1

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "MIRecord": {
        "*": {
          "NameValue": {
            "*": {
              "Name": "CHANGE1"
            }
          }
        }
      }
    }
  }
]

I don’t know if I’m using the right function to replace or if I need to rebuild the whole json. But I’d like to avoid having to redo the json because the input JSON isn’t static.

Does anyone have a solution please?

2

Answers


  1. Chosen as BEST ANSWER

    here's my screenshot, where you can see that the JOLT isn't working as expected

    enter image description here


  2. To replace the values of MBWHLO, MBITNO, and V_NETA in your JSON with CHANGE1, CHANGE2, and CHANGE3, respectively, you can use the following Jolt transformation:

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "MIRecord": {
            "*": {
              "NameValue": {
                "*": {
                  "Name": {
                    "MBWHLO": "CHANGE1",
                    "MBITNO": "CHANGE2",
                    "V_NETA": "CHANGE3"
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    This transformation will use the modify-overwrite-beta operation to overwrite the values of the Name fields for the MBWHLO, MBITNO, and V_NETA fields with the corresponding values of CHANGE1, CHANGE2, and CHANGE3.

    For example, if you apply this transformation to your input JSON, you will get the following output JSON:

    {
      "MIRecord": [
        {
          "RowIndex": "0",
          "NameValue": [
            {
              "Name": "CHANGE1",
              "Value": "123"
            },
            {
              "Name": "CHANGE2",
              "Value": "123"
            },
            {
              "Name": "CHANGE3",
              "Value": "123"
            }
          ]
        },
        {
          "RowIndex": "1",
          "NameValue": [
            {
              "Name": "CHANGE1",
              "Value": "123"
            },
            {
              "Name": "CHANGE2",
              "Value": "123"
            },
            {
              "Name": "CHANGE3",
              "Value": "123"
            }
          ]
        }
      ]
    }
    

    You can use this Jolt transformation to replace the values of MBWHLO, MBITNO, and V_NETA in your JSON without having to rebuild the entire JSON.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search