JSON modification: Changing the values of multiple elements using JSONPATH

180 Views Asked by At

I am using jsonPath to access the values of a json string. Using json path I get a list of values which I have to modify and again set them back to their corresponding locations. When I try to do that Jsonpath sets all the values in all the locations. I want the correct value in its correct location. How to do that?

I am using the jsonpath: "employees[*].name" and using DocumentContext.set("employees[*].name", List<String> values);

JSON:
{
"employees" : [ {
    "name" : "Rama",
    "email" : "[email protected]",
    "age" : 23
  }, {
    "name" : "Shya",
    "email" : "[email protected]",
    "age" : 28
  }, {
    "name" : "John",
    "email" : "[email protected]",
    "age" : 33
  }, {
    "name" : "Boby",
    "email" : "[email protected]",
    "age" : 41
  } ]
}

WHAT I AM GETTING:

{
"employees" : [ {
    "name" : "[1aRama,3jShya,j5John,ytBoby]",
    "email" : "[email protected]",
    "age" : 23
  }, {
    "name" : "[1aRama,3jShya,j5John,ytBoby]",
    "email" : "[email protected]",
    "age" : 28
  }, {
    "name" : "[1aRama,3jShya,j5John,ytBoby]",
    "email" : "[email protected]",
    "age" : 33
  }, {
    "name" : "[1aRama,3jShya,j5John,ytBoby]",
    "email" : "[email protected]",
    "age" : 41
  } ]
}

WHAT I WANT:

{
"employees" : [ {
    "name" : "1aRama",
    "email" : "[email protected]",
    "age" : 23
  }, {
    "name" : "3jShya",
    "email" : "[email protected]",
    "age" : 28
  }, {
    "name" : "j5John",
    "email" : "[email protected]",
    "age" : 33
  }, {
    "name" : "ytBoby",
    "email" : "[email protected]",
    "age" : 41
  } ]
}
0

There are 0 best solutions below