python-json-patch
python-json-patch copied to clipboard
How to get test patch?
Given:
d1 = {1: "a", 2: "b"}
d2 = {1: "b", 2: "v"}
patch = jsonpatch.make_patch(d1, d2)
patch.to_string()
'[{"value": "b", "op": "replace", "path": "/1"}, {"value": "v", "op": "replace", "path": "/2"}]'
How can I get the patch that would have test
instead of replace
?
https://python-json-patch.readthedocs.io/en/latest/mod-jsonpatch.html#jsonpatch.make_patch
does apply()
not work?
@Xronophobe Im not sure I understand what you mean? My use case is to have jsonpatch generate a patch that I send to a remote API in a PATCH operation. Right now Im only able to get a patch object that has replace
but Id like to get the patch to have test
in it as well so I can submit it first to make sure the API would be happy, then if I get a success response from my test
patch I can follow up with another call that has the real patch in it with replace
The API that I'm working against also requires a test operation on the existing value before any other operation can take place. As a work-around for this, I switched around the old and new document and changed the "op" to "test" on the resulting patch document. Maybe there's a smarter way?