Given the JSON object
{
"property": "value"
}
If you perform a JSON patch on this object like the following
[{
"op": "add",
"path": "/otherProperty/property",
"value": "childvalue"
}]
Should the JSON patch operation fail because the otherProperty
isn't defined or should the operation add the whole path?
I couldn't find any information in this.
As mentioned in the comments, the JSON Patch Internet Draft states that the operation should result in an error:
However, the object itself or an array containing it does need to
exist, and it remains an error for that not to be the case. For
example, an "add" with a target location of "/a/b" starting with this
document:
{ "a": { "foo": 1 } }
is not an error, because "a" exists, and "b" will be added to its
value. It is an error in this document:
{ "q": { "bar": 2 } }
because "a" does not exist.
That said you can still do what you want, but you have to change the syntax by adding an object that contains the property you want. So according to Appendix 10 of that draft you can do
[{
"op": "add",
"path": "/otherProperty",
"value": { "property" : "childvalue" }
}]
In this case you are creating a field at the root level that has a json object as body:
{
"property": "value",
"otherProperty" : {
"property" : "childvalue"
}
}
I tested this here by pasting before and after JSON of the target resource, and it generated the the same add statement I presented above.