'Java JsonPath: modify multiple JSON attributes at once

I am struggling to modify several JSON objects, while using the JsonPath library. I have a selector, that gets declared at runtime and a JSON file of unknown size. I want to only modify the JSON objects, that match the filter’s criteria defines by the selector. In this example, all objects get a new key: joinedName = firstName + ‘ ‘ + lastName (except Bob’s object). I did get it to work, but I have a feeling, that there should be a better way of doing this. Please let me know how to improve my workaround. Thanks!

@Test
public void addMultipleValuesAtOnce() throws JSONException {

    String json = "{\n" +
            "\"jsonArr\": [\n" +
            "   {\n" +
            "       \"firstName\":\"Alice\",\n" +
            "       \"lastName\":\"Smith\"\n" +
            "   },\n" +
            "   {\n" +
            "       \"firstName\":\"Bob\",\n" +
            "       \"lastName\":\"Williams\"\n" +
            "   },\n" +
            "   {\n" +
            "       \"firstName\":\"Carol\",\n" +
            "       \"lastName\":\"Johnson\"\n" +
            "   }\n" +
            "   ]\n" +
            "}";

    System.out.println("------------- IN --------------");
    System.out.println(json);
    DocumentContext parse = JsonPath.parse(json);

    final String selector = "$.jsonArr[?(!(@.firstName == 'Bob'))]"; // select every entry, except BOB's

    net.minidev.json.JSONArray firstNames = parse.read(selector + ".firstName");
    net.minidev.json.JSONArray lastNames = parse.read(selector + ".lastName");

    //HACK
    {
        String[] joinedNames = new String[firstNames.toArray().length];
        for (int i = 0; i < joinedNames.length; i++) {
            joinedNames[i] = firstNames.get(i) + " " + lastNames.get(i);
        }

        parse.put(selector, "joinedName", null); // add key
        final AtomicReference<Integer> counter = new AtomicReference<>(0);
        parse.map(selector + ".joinedName", (o, configuration) -> {
           return joinedNames[counter.getAndUpdate(x -> x + 1)];
        });
    }

    System.out.println("------------- OUT --------------");
    System.out.println(parse.jsonString());

    // The expected values:
    Assert.assertEquals("Alice Smith", parse.read("$.jsonArr[0].joinedName"));
    Assert.assertEquals("Carol Johnson", parse.read("$.jsonArr[2].joinedName"));
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source