Home How can update the object in array by PATCH with REST API if the array is null?
Reply: 0

How can update the object in array by PATCH with REST API if the array is null?

Hoa Cao
1#
Hoa Cao Published in 2018-01-12 10:25:56Z

I get an issue don't know how to update an object in the array by PATCH with REST API in case the array is null.

EX: - I have a contact with the model as below:

 {
    "id":"",
    "first_name": "",
    "last_name": "",
    "addresses": [
     {
      "first_address": "",
      "city": "",
     }    
     ]
}
  • How can I update the first_address, city with using PATCH if "addresses":null ?

    {
    "id":"090909",
    "first_name": "Ellen",
    "last_name": "Cao",
    "addresses": null
    

    }

  • Always get the error: Request by PATCH

     [{
      "op": "replace",
      "path": "/first_name",
      "value": "H"
    },
    {
      "op": "replace",
      "path": "/last_name",
      "value": "Update"
    },
    {
      "op": "replace",
      "path": "/addresses/0/first_address",
      "value": "Tran Hung Dao"
    },
    {
      "op": "replace",
      "path": "/addresses/0/city",
      "value": "HCM"
    }
    ]
    

    Returned error: { "message": "For operation 'replace', the target location specified by path '/addresses/0/first_address' was not found (/addresses/0/first_address)." }

    • Also get error if using "op" is "add".

      [{ "op": "add", "path": "/addresses/-/first_address", "value": "Tran Hung Dao" }, { "op": "add", "path": "/addresses/-/city", "value": "HCM" }]

Returned error: { "message": "For operation 'add', the target location specified by path '/addresses/-/first_address' was not found (/addresses/-/first_address)." } Is there any way to add "adresses" by using PATCH ? Thanks so much if someone can help me.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331496 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO