Recommended library for manipulating JSON?

I have a scenario where the service is providing me a JSON object. I am expected to manipulate that object and send it back.

I would like a generalized solution, one that does not require me to build a Scala object to deserialize the JSON into. I just want to manipulate the JSON in place.

Because I’m experienced with RegEx, I’m hesitant to go down that route, because I know how problematic that can be.

I could cheat, and just hard-code the response, but then the code becomes brittle. I much prefer to do what I know the real client does.

Anybody have a recommendation on a library/approach for JSON manipulation? For instance, can JsonPath be used for manipulation, and not just extraction?

Thanks for any advice you may have.

– John

https://gatling.io/2019/07/31/introducing-jmespath-support/

That looks fantastic. There’s just one catch: I can’t find in the documentation anything on how to do surgical substitutions within the JSON and otherwise return the JSON as-is. It seems to be all about transforming it down to something smaller, simpler. I can’t find anything for how to tweak something in place. map() looks promising, but I can’t make it work.

As a simple example, using the sample JSON on the JMESPath home page:

{
“locations”: [
{“name”: “Seattle”, “state”: “WA”},
{“name”: “New York”, “state”: “NY”},
{“name”: “Bellevue”, “state”: “WA”},
{“name”: “Olympia”, “state”: “WA”}
]
}

What would the expression be to change Seattle’s state to NM and New York’s state to HI, so that the result looks like this:

{
“locations”: [
{“name”: “Seattle”, “state”: “NM”},
{“name”: “New York”, “state”: “HI”},
{“name”: “Bellevue”, “state”: “WA”},
{“name”: “Olympia”, “state”: “WA”}
]
}

What I’m looking for is to avoid having any more knowledge embedded in the expression than is necessary to make the transformation. I don’t want to have to know the full structure of the document that I’m transforming, because I want the document to be able to change and not have to care, so long as the part I’m modifying remains consistent.

I don’t think you can do that with JMESPath, but you should probably ask on StackOverFlow.