How to Remove the Extra Square Bracket from Json Having Multiple Arrays

How do I remove the extra square bracket from JSON having multiple arrays

You can merge arrays while getting

$episode = array_merge($episode, $podcast->getPodcastByCategoryId($id)); 

Or flatten the result array before converting to json

echo json_encode(array_merge(...$episode));

demo

How to Remove Square bracket from JSON

var tmpStr = '[    
{
"Name": "TEST",
"deviceId": "",
"CartId": "",
"timestamp": 1383197265540,
"FOOD": [],
"City": "LONDON CA"
}

]';

var newStr = tmpStr.substring(1, tmpStr.length-1);

See this codepen example

Remove extra brackets from JSON call in Integromat

You can use the replace function and insert the brackets that need to be found using regex pattern making sure you denote the bracket at the starting position and the bracket at the end of the string to be replaced with emptystring

How to avoid double square brackets under JSON Array while retriving data from MAP

if key is not there at first time then your are adding an empty JSONArray and later you are fetching that empty array and adding your created array in it.

if the key is there then fetch the already created array and add new array in it.

for(int i=0;i<2;i++)
{
JsonArray jsonarray = new JsonArray();
JsonObject pacs_obj = new JsonObject();
pacs_obj.addProperty("pac_id", "1235");
jsonarray.add(pacs_obj);
String new_catid = "1";

if (datafromdb.containsKey(new_catid)) {
datafromdb.get(new_catid).add(jsonarray);
}
else{
datafromdb.put(new_catid, jsonarray);
}
}

remove outer quote from json array

You have to build the json object-by-object. putting it all in one String does not work:

JSONObject obj = new JSONObject();
obj.put("Name", "stack");
obj.put("xyz", "something");
JSONArray jsonArray= new JSONArray();
jsonArray.add(obj);
// same for 2nd item

How to remove the JSON array, brackets, key and value using replaceAll method?

There usually is no reason to that with string replacements -- it has just to much potential to mess something up. You can just modify the map before writing it back as JSON. E.g.:

import groovy.json.*

def jsonStr = '{"a": 1, "b": [{"c": 3, "d": 4}]}}'
def json = new JsonSlurper().parseText(jsonStr)
// XXX: first "de-array" `b`
json.b = json.b.first()
// next remove `c` from it
json.b.remove('c')
println JsonOutput.toJson(json)
// => {"a":1,"b":{"d":4}}

edit:

OP also wants to get rid of the array, altough this messes with the naming and only works if there is at least one element (see comments)



Related Topics



Leave a reply



Submit