I’ve below JSON and I wanted to update few fields from it
{ "process": "my-process", "pod": "some-pod", "org": "some-org", "config": { "version": "436_601_83.0.0", "path": "companyName/ccg", "description": "update the version", "dependencies": null } }
using postman PATCH API call with below JSONPatch payload API is working fine
[ { "path": "/config/version", "op": "replace", "value": "436_605_83.0.0" }, { "path": "/config/description", "op": "replace", "value": "foo bar" } ]
But, I want to implement same using Java .. I tried
JsonPatch jsonPatch = new JsonPatch( Arrays.asList( new ReplaceOperation(JsonPointer.of("/config/version"), new TextNode("436_605_83.0.0")) ) );
It evaluates to :
[{"op":"replace","path":"/~1config~1version","value":"436_605_83.0.0"}]
This doc mentioned that http://jsonpatch.com/#json-pointer we MUST use escape the characters with ~0
and ~1
but no luck yet, I escaped /
using ~1
i.e. "~1config~1version"
but it evaluates to "/~01config~01version"
Advertisement
Answer
I think the problem is in the JsonPointer
definition. Please, try something like that instead:
JsonPatch jsonPatch = new JsonPatch( Arrays.asList( new ReplaceOperation( // Note we should provide the different paths tokens here JsonPointer.of("config", "version"), new TextNode("436_605_83.0.0") ) ) );
Or, equivalently:
JsonPatch jsonPatch = new JsonPatch( Arrays.asList( new ReplaceOperation( // Create the JsonPointer with the full path new JsonPointer("/config/version"), new TextNode("436_605_83.0.0") ) ) );
Please, see this test, it provides guidance about how to tactically build JsonPointer
s and the meaning of escaping reserved chars.