Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete object in JSON?

Tags:

json

php

I have a JSON archive and I want find and delete one object. How to find and delete object in a JSON archive?

Example:

{
         "themes":[

                  {"name":"pepito",
                   "thumbnail":"http://nakome.16mb.com/samples/html/admin/",
                   "description":"Simple"},

                  // Delete this in php ---------
                  {"name":"juanito",  
                   "thumbnail":"http://nakome.16mb.com/samples/html/admin/",
                   "description":"Simple" }
                  // ----------------------------

                 ]
}
like image 561
Nakome Avatar asked Dec 10 '12 14:12

Nakome


People also ask

How do I remove a specific element from a JSON array?

You can remove an element from the JSONArray object using the remove() method. This method accepts an integer and removes the element in that particular index.

What is toJSON () in JSON?

toJSON() calls the object's toISOString() method, which returns a string representing the Date object's value. This method is generally intended to, by default, usefully serialize Date objects during JSON serialization, which can then be deserialized using the Date() constructor or Date. parse() as the reviver of JSON.

How do you remove a key value pair from a JSON object in Java?

JsonObject::remove() removes a key-value pair from the object pointed by the JsonObject . If the JsonObject is null, this function does nothing.


1 Answers

Without having to write your own parser (and given it's php) use json_decode and turn it in to an object you want to work with, use unset then re-json_encode the result.

That would be the easiest way without having to perform string parsing/searches.

like image 193
Brad Christie Avatar answered Sep 22 '22 08:09

Brad Christie