-1

In my React application, I'm using useForm to sending my forms. When I'm using the PATCH method and sending to API a value (array):

participants.push({ participant_id: person, place_id: place });

then my PATCH request looks like this:

{
    "patch": [
        {
            "op": "add",
            "path": "/participants",
            "value": [
                {
                    "participant_id": "e4c0958d-4e08-405e-1cab-d3447dd1cf04",
                    "place_id": "853719d0-8a14-45f8-1b02-b352209ab340"
                },
                {
                    "participant_id": "ae2ad2e9-47ad-1604-86e7-39edc4f8cba1",
                    "place_id": "97671302-0b53-418b-4714-5b96fb67c836"
                },
                {
                    "participant_id": "e4e70581-ef51-470c-b674-51487e9e97ed",
                    "place_id": "853719d0-8an4-45f8-9b02-b352209ab340"
                },
            ]
        }
    ]
}

and I need to make a single operation for each of element of my value array, I need it looks like this:

{
        "op": "replace",
        "path": "/participants/0",
        "value": {
            "participant_id": "27311fb1-a1be-4a2f-a64a-05ad93e204b4",
            "place_id": "7eb3a7d7-bf57-4d3c-8983-3398ffe3b241"
        }
    },
    {
        "op": "replace",
        "path": "/participants/1",
        "value": {
            "participant_id": "aa676d70-49d6-4302-8de8-9ea0bccc0d2d",
            "place_id": "7eb3a7d7-bf57-4d3c-8983-3398ffe3b241"
        }
    },
    {
        "op": "replace",
        "path": "/participants/2",
        "value": {
            "participant_id": "5455db6f-d0ed-41ad-838a-da06e047a665",
            "place_id": "7eb3a7d7-bf57-4d3c-8983-3398ffe3b241"
        }
    }

How to approach this ? how to structure the value to get the desired effect?

I have tried to map through my value array and for each element define a new object with "op", "path" and "value" but I still get single operation request with nested objects:

{
    "patch": [
        {
            "op": "add",
            "path": "/participants",
            "value": [
                {
                     "op": "add",
                     "path": "/participants",
                     "value":                
                        {"participant_id": "e4c0958d-4e08-405e-1cab-d3447dd1cf04",
                        "place_id": "853719d0-8a14-45f8-1b02-b352209ab340"},
                },
                {
                     "op": "add",
                     "path": "/participants",
                     "value":                
                        {"participant_id": "e4c0958d-4e08-405e-1cab-d3447dd1cf04",
                        "place_id": "853719d0-8a14-45f8-1b02-b352209ab340"},
                },
    ]
}
Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 28 '23 at 22:44

1 Answers1

2

Sorry, I didn't get the question completely, but if the ask is transforming the given format to the desired format, then you can simply iterate over participants and for each create an object of patch operation and push it into an array.

 participants.push(
  { op: 'replace',
    path: 'participants/'+id,
    value: {participant_id: person, place_id: place }
  });

Below is snippet to give you an example

let x = {
  "patch": [{
    "op": "add",
    "path": "/participants",
    "value": [{
        "participant_id": "e4c0958d-4e08-405e-1cab-d3447dd1cf04",
        "place_id": "853719d0-8a14-45f8-1b02-b352209ab340"
      },
      {
        "participant_id": "ae2ad2e9-47ad-1604-86e7-39edc4f8cba1",
        "place_id": "97671302-0b53-418b-4714-5b96fb67c836"
      },
      {
        "participant_id": "e4e70581-ef51-470c-b674-51487e9e97ed",
        "place_id": "853719d0-8an4-45f8-9b02-b352209ab340"
      }
    ]
  }]
};

// Creating Array to store the transformed JSON
ans = [];

// transform the JSON in desired format
x.patch.forEach(function(obj) {
  for (let index in obj.value) {
    ans.push({
      "op": "replace",
      "path": "/participants/" + index,
      "value": {
        "participant_id": obj.value[index].participant_id,
        "place_id": obj.value[index].place_id,
      }
    });
  }
});
console.log(ans);
Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
Deepak Jangra
  • 49
  • 1
  • 5