Try this code

ArrayList<String> list = new ArrayList<String>();     
JSONArray jsonArray = (JSONArray)jsonObject; 

if (jsonArray != null) { 
   int len = jsonArray.length();
   for (int i=0;i<len;i++){ 
    list.add(jsonArray.get(i).toString());
   } 
}
//Remove the element from arraylist
list.remove(position);
//Recreate JSON Array
JSONArray jsArray = new JSONArray(list);

Edit: Using ArrayList will add "\" to the key and values. So, use JSONArray itself

JSONArray list = new JSONArray();     
JSONArray jsonArray = new JSONArray(jsonstring); 
int len = jsonArray.length();
if (jsonArray != null) { 
   for (int i=0;i<len;i++)
   { 
       //Excluding the item at position
        if (i != position) 
        {
            list.put(jsonArray.get(i));
        }
   } 
}
Answer from Vinothkumar Arputharaj on Stack Overflow
Top answer
1 of 11
49

Try this code

ArrayList<String> list = new ArrayList<String>();     
JSONArray jsonArray = (JSONArray)jsonObject; 

if (jsonArray != null) { 
   int len = jsonArray.length();
   for (int i=0;i<len;i++){ 
    list.add(jsonArray.get(i).toString());
   } 
}
//Remove the element from arraylist
list.remove(position);
//Recreate JSON Array
JSONArray jsArray = new JSONArray(list);

Edit: Using ArrayList will add "\" to the key and values. So, use JSONArray itself

JSONArray list = new JSONArray();     
JSONArray jsonArray = new JSONArray(jsonstring); 
int len = jsonArray.length();
if (jsonArray != null) { 
   for (int i=0;i<len;i++)
   { 
       //Excluding the item at position
        if (i != position) 
        {
            list.put(jsonArray.get(i));
        }
   } 
}
2 of 11
20

In case if someone returns with the same question for Android platform, you cannot use the inbuilt remove() method if you are targeting for Android API-18 or less. The remove() method is added on API level 19. Thus, the best possible thing to do is to extend the JSONArray to create a compatible override for the remove() method.

public class MJSONArray extends JSONArray {

    @Override
    public Object remove(int index) {

        JSONArray output = new JSONArray();     
        int len = this.length(); 
        for (int i = 0; i < len; i++)   {
            if (i != index) {
                try {
                    output.put(this.get(i));
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
            }
        } 
        return output;
        //return this; If you need the input array in case of a failed attempt to remove an item.
     }
}

EDIT As Daniel pointed out, handling an error silently is bad style. Code improved.

🌐
ArduinoJson
arduinojson.org › version 6 › api › jsonarray › remove()
JsonArray::remove() | ArduinoJson 6
deserializeJson(doc, input); JsonArray crew = doc["survivors"]; crew.remove(1); serializeJsonPretty(object, Serial);
🌐
Progress
docs.progress.com › bundle › abl-reference › page › Remove-method-JsonArray.html
Remove( ) method (JsonArray)
February 10, 2026 - Skip to main contentSkip to search · Powered by Zoomin Software. For more details please contactZoomin
🌐
TutorialsPoint
tutorialspoint.com › remove-json-element-javascript
Remove json element - JavaScript?
November 3, 2023 - To remove entire objects from the JSON array, use array methods like splice():
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray.remove java code examples | Tabnine
JSONObject root = new JSONObject(json); JSONArray myData = (JSONArray) root.get("myData"); myData.remove(0); myData.put(4); System.out.println("root = " + root.toString()); ... //instantiate your json array (e.g. from a string, or a file) //String ...
🌐
Quora
quora.com › How-do-I-remove-a-JSON-object-from-a-JSON-array
How to remove a JSON object from a JSON array - Quora
Answer: There are no such things. JSON uses human-readable text to encode data. Some strong-typed languages such as Java define a JSONObject class and a JSONArray class, but those are data types defined by the JSON encoding and decoding library ...
🌐
Stack Overflow
stackoverflow.com › questions › 36325499 › remove-json-object-from-json-array
java - Remove json object from json array - Stack Overflow
I tried using org.json library as mentioned by Sanj in the above post. We can remove the element from JSONArray as below. I placed the json content in the .txt file and read into the String object for constructing the JSONObject.
Find elsewhere
🌐
Baeldung
baeldung.com › home › json › jackson › removing json elements with jackson
Removing JSON Elements With Jackson | Baeldung
January 8, 2024 - In some cases, we may encounter ... nested objects or arrays. Handling these structures efficiently requires the ability to remove specific elements based on our requirements. By using Jackson’s rich set of APIs, we can iterate over the elements of a JsonNode instance and perform conditional checks to identify elements for removal. To remove elements from nested objects ...
🌐
Processing
processing.org › reference › JSONArray_remove_.html
remove() / Reference / Processing.org
January 1, 2021 - // // [ // { // "id": 0, // "species": "Capra hircus", // "name": "Goat" // }, // { // "id": 1, // "species": "Panthera pardus", // "name": "Leopard" // }, // { // "id": 2, // "species": "Equus zebra", // "name": "Zebra" // } // ] JSONArray values; void setup() { values = loadJSONArray("data.json"); values.remove(0); // Remove the array's first element for (int i = 0; i < values.size(); i++) { JSONObject animal = values.getJSONObject(i); int id = animal.getInt("id"); String species = animal.getString("species"); String name = animal.getString("name"); println(id + ", " + species + ", " + name); } } // Sketch prints: // 1, Panthera pardus, Leopard // 2, Equus zebra, Zebra · .remove(index) index(int)the index value of the element to be removed · Object ·
Top answer
1 of 1
11

To remove all elements from the column images (holding a json array) where 'id' is 'note_1':

pg 9.3

UPDATE items i
SET    images = i2.images
FROM  (
  SELECT id, array_to_json(array_agg(elem)) AS images
  FROM   items i2
       , json_array_elements(i2.images) elem
  WHERE  elem->>'id' <> 'note_1'
  GROUP  BY 1
  ) i2
WHERE  i2.id = i.id
AND    json_array_length(i2.images) < json_array_length(i.images);

SQL Fiddle.

Explain

  1. Unnest the JSON array with json_array_elements() in a subquery using an implicit JOIN LATERAL for the set-returning function. Details:
    • PostgreSQL joining using JSONB
    • How to turn json array into postgres array?
  2. JOIN to the base table and add another WHERE condition using json_array_length() to exclude unaffected rows - so you don't update each and every row of the table, which would be expensive (and wide-spread) nonsense.

pg 9.4

This gets much easier with jsonb and additional jsonb operators.


UPDATE items i
SET    images = i2.images
FROM  (
  SELECT id, array_to_json(array_agg(elem)) AS images
  FROM   items cand
       , json_array_elements(cand.images) elem
  WHERE  cand.images @> '{[{"id":"note_1"}]}'::jsonb
  AND    elem->>'id' <> 'note_1'
  GROUP  BY 1
  ) i2
WHERE i2.id = i.id;

Eliminates unaffected rows at the start, which is much faster. Plus, there is extensive native index support for jsonb, too, now.

Here are some example, benchmarks and comparison of new features to old json and MongoDB, plus outlook to jsquery by (some of) their main authors, Alexander Korotkov, Oleg Bartunov andTeodor Sigaevat PGCon 2014:

  • "CREATE INDEX ... USING VODKA"
🌐
GitHub
gist.github.com › gkhays › 4fe1e6193e62b1f2cad3bd4b00f16c92
Remove an attribute or element from a JSON array during enumeration · GitHub
July 18, 2018 - JSONArray nameArray = firstJSON.names(); List<String> keyList = new ArrayList<String>(); for (int i = 0; i < nameArray.length(); i++) { keyList.add(nameArray.get(i).toString()); } for (int i = 0; i < ja.length(); i++) { for (String key : keyList) { JSONObject json = ja.getJSONObject(i); if (json.getString(key).equals("null")) { json.remove(key); } } }
🌐
ArduinoJson
arduinojson.org › version 6 › api › jsonobject › remove()
JsonObject::remove() | ArduinoJson 6
Here is how you can remove “Ellis” from the crew: deserializeJson(doc, input); JsonObject crew = doc["survivors"]; crew.remove("Ellis"); serializeJsonPretty(object, Serial);