0

I have a API which returns the JSON data something like this:

{
  "data":{
    "id": "859545",
    "name":"Batman",
    "custom-fields": {
      "--f-09":"custom-1",
      "--f-10":"custom-2",
      "--f-11":"custom-3"
    },
    "tags": [],
    "created-at": "2021-09-10T15:45:16Z",
    "updated-at": "2022-04-23T11:52:49Z"
  }
}

For this JSON I would like to change the field "--f-09" to "custom-1, custom-new" and "--f-10" to "custom-2, custom-new" while keeping all other fields as before.

I am aware that I can use request.PATCH in Nodejs for this but in that case, I need to provide all the data again for the request which I would like to avoid. I just want to update certain fields while keeping others as before.

In this example, I have provided a simple example which contains only certain fields but in my real code I have many fields so does this mean that I need to build the response body json using all the fields again and just change the --f-09 and --f-10?

Following is the code:

const jsonBody = {
  "data": {
    "id": "859545",
    "name": "Batman",
    "custom-fields": {
      "--f-09": "custom-1, custom-new",
      "--f-10": "custom-2, custom-new",
      "--f-11": "custom-2"
    },
    "tags": [],
    "created-at": "2021-09-10T15:45:16Z",
    "updated-at": "2022-04-23T11:52:49Z"
  }
}


request.patch('https://reqres.in/api/users',jsonBody,function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
        console.log(response.statusCode);
    }
  }
);

Does this mean that I need to build the complete JSON body again here just like I have built the jsonBody above while using the PATCH or is there any other way where I can just pass the value for --f-09 and --f-10?

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
  • _I am aware that I can use `request.PATCH` in `Nodejs`..._ are you also aware that you can parse the json, and simply change the fields with code? See https://stackoverflow.com/questions/4647817/javascript-object-rename-key – smac89 Apr 04 '22 at 16:52

1 Answers1

0

First you check, what parameters do you want update is side the object. then you can filter , that values only. then you can merge separate parts in the object.