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
How to Get the Values of Input Using PHP
Object of Class Mysqli_Result Could Not Be Converted to String
How to Unlink (Delete) an Image in Codeigniter
How to Install and Use PHP on Macos Via Brew
Redirect That Same Page from Where Id Is Coming
How to Change Default Url from Localhost:8000 to Other Ip in Laravel When We Run "Php Artisan Serve"
Get Total Number of Members in Discord Using PHP
Trim Country Code from Phone Number in PHP
Checking If Email Is in MySQL Database in PHP
Laravel Ajax Response Return HTML Elements
Saving Multiple Records in a Laravel Eloquent Create
Codeigniter: Fatal Error Call to Undefined Function Mysqli_Init()
Remove Comma on Last Item of Foreach
Getting Data from a Textarea into a Database
What Is the Regular Expression to Validate a Comma Delimited List But Ending With '&' and a Word