Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple values to array in single JSON Patch operation?

I have a json object like this:

{
    "content" : [
        {
            "id" : 54
            "foo" : "bar"
        },
        {
            "id" : 43
            "foo" : "bar"
        },
        {
            "id" : 76
            "foo" : "bar"
        }
    ]
}

If I want to add multiple items to the content array (order doesn't matter) can I add to it with a json patch with a single line/ operation with something like this?

{ "op": "add", "path": "/content/-", "value": [
        {
            "id" : 34
            "foo" : "bar"
        },
        {
            "id" : 23
            "foo" : "bar"
        },
        {
            "id" : 87
            "foo" : "bar"
        }
    ] 
}

Or do I have to do an additional line for each object I want to add?

EDIT: To be clear I want to append, not replace the content.

like image 528
Blasterdude8 Avatar asked Jan 21 '20 21:01

Blasterdude8


People also ask

Can JSON have multiple values?

This processor extracts multiple values from a field containing a JSON array and concatenates them into a multivalued field.

How does JSON Patch work?

How it works. A JSON Patch document is just a JSON file containing an array of patch operations. The patch operations supported by JSON Patch are “add”, “remove”, “replace”, “move”, “copy” and “test”. The operations are applied in order: if any of them fail then the whole patch operation should abort.

What is content type application JSON Patch JSON?

JSON Patch is a web standard format for describing changes in a JSON document. It is meant to be used together with HTTP Patch which allows for the modification of existing HTTP resources. The JSON Patch media type is application/json-patch+json .

What is a JSON Patch document?

JSON Patch is a format for specifying updates to be applied to a resource. A JSON Patch document has an array of operations. Each operation identifies a particular type of change. Examples of such changes include adding an array element or replacing a property value.


1 Answers

Unfortunately, this seems not (yet?) possible... as the Json Patch specification states :

https://www.rfc-editor.org/rfc/rfc6902#section-4.1

The "add" operation performs one of the following functions, depending upon what the target location references:

  • If the target location specifies an array index, a new value is inserted into the array at the specified index.

  • If the target location specifies an object member that does not already exist, a new member is added to the object.

  • If the target location specifies an object member that does exist, that member's value is replaced.

like image 129
T.M. Avatar answered Sep 19 '22 12:09

T.M.