0
{

    "employeeIdentifiers": [
        {
            "type": "EmployeeProductID",
            "finalIdentifier": "464646"
        },
        {
            "type": "EmployeeDomainID",
            "finalIdentifier": "2424225"
        }
    ],
    "employeeStatus": [
        {
            "employeeType": "Employee",
            "employeeSUBIdentifiers": [{
                "type": "mainID",
                "code": "10224242424"
            }]


        },
        {
            "employeeType": "Customer",
            "employeeSUBIdentifiers": [{
                    "type": "subordinateID",
                    "code": "468668686"
                },
                {
                    "type": "mainID",
                    "code": "468668686"
                }
            ]
        }
    ]

}

Above JSON string is coming in the method and I want to replace the text of a node.

In the "employeeStatus" array, if employeeType is 'Employee' and employeeSUBIdentifiers[0].code value should be changed from "10224242424" to "9924242424". How to modify/replace the value using Jackson library?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user739115
  • 1,117
  • 5
  • 20
  • 41
  • 2
    What problem are you having doing this, exactly? – tgdavies Dec 27 '22 at 03:26
  • 3
    Just parse the JSON into an in-memory structure, make the change you want to make to the structure, then write out the modified structure as JSON. I don't see any reason to use any sort of library other than the basic Jackson JSON decoder and encoder. – CryptoFool Dec 27 '22 at 03:40
  • 1
    You don't have to define a complex class/object-model. Could read as [`JsonNode`](https://stackoverflow.com/questions/3653996/how-to-parse-a-json-string-into-jsonnode-in-jackson) then [modify](https://stackoverflow.com/questions/30997362/how-to-modify-jsonnode-in-java#30997696). – hc_dev Dec 28 '22 at 18:38

1 Answers1

0

You may try library Josson.

https://github.com/octomix/josson

disclaimer: I am the author of Josson

Deserialization

Josson josson = Josson.fromJsonString(
    "{" +
    "    \"employeeIdentifiers\": [" +
    "        {" +
    "            \"type\": \"EmployeeProductID\"," +
    "            \"finalIdentifier\": \"464646\"" +
    "        }," +
    "        {" +
    "            \"type\": \"EmployeeDomainID\"," +
    "            \"finalIdentifier\": \"2424225\"" +
    "        }" +
    "    ]," +
    "    \"employeeStatus\": [" +
    "        {" +
    "            \"employeeType\": \"Employee\"," +
    "            \"employeeSUBIdentifiers\": [{" +
    "                \"type\": \"mainID\"," +
    "                \"code\": \"10224242424\"" +
    "            }]" +
    "        }," +
    "        {" +
    "            \"employeeType\": \"Customer\"," +
    "            \"employeeSUBIdentifiers\": [{" +
    "                    \"type\": \"subordinateID\"," +
    "                    \"code\": \"468668686\"" +
    "                }," +
    "                {" +
    "                    \"type\": \"mainID\"," +
    "                    \"code\": \"468668686\"" +
    "                }" +
    "            ]" +
    "        }" +
    "    ]" +
    "}");
            

Transformation

JsonNode node = josson.getNode(
    "field(employeeStatus" +
    "      .findAndModify([employeeType='Employee' & employeeSUBIdentifiers[0].code='10224242424']," +
    "                     field(employeeSUBIdentifiers" +
    "                           .findAndModify(0, field(code:'9924242424'))))" +
    ")");
System.out.println(node.toPrettyString());

Output

{
  "employeeIdentifiers" : [ {
    "type" : "EmployeeProductID",
    "finalIdentifier" : "464646"
  }, {
    "type" : "EmployeeDomainID",
    "finalIdentifier" : "2424225"
  } ],
  "employeeStatus" : [ {
    "employeeType" : "Employee",
    "employeeSUBIdentifiers" : [ {
      "type" : "mainID",
      "code" : "9924242424"
    } ]
  }, {
    "employeeType" : "Customer",
    "employeeSUBIdentifiers" : [ {
      "type" : "subordinateID",
      "code" : "468668686"
    }, {
      "type" : "mainID",
      "code" : "468668686"
    } ]
  } ]
}
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8
  • Want to promote your library? I see two issues: (1) *software recommendations are off topic* on Stack Overflow, see [help/on-topic]; (2) as the author you should add a "disclaimer: I am the author of Josson", see [help/behavior], last section "Avoid overt self-promotion.". Or [discuss on meta](https://meta.stackoverflow.com/questions/285851/user-promoting-his-software-in-relevant-questions-without-disclosure/) – hc_dev Dec 28 '22 at 18:50