How to remove the Square Bracket in JSON using PHP

remove square brackets from json
remove square brackets from array php
square brackets in json php
how to remove array from json in php
remove curly braces from array in php
remove in json php
json array without brackets
how to add square brackets in json

Below code is the returned JSON


Below code is the desired result of the returned JSON (without the array bracket)


Below code is to convert the array to the JSON format

$result = mysql_query("SELECT * FROM patientvaccinedetail"); 
$specific = []; 
while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) { 
echo "<tr>"; 
echo '<td width="100px">' . $row['id'] . '</td>'; 
echo '<td width="200px">' . $row['patientid'] . '</td>'; 
echo '<td width="200px">' . $row['vaccineid'] . '</td>'; 
  //**********Convert the array into json******************* 

  $specific[] = ["one" => $row["id"],
                 "two" => $row["patientid"],
                 "three" => $row["vaccineid"]];

$result = json_encode($specific,JSON_UNESCAPED_UNICODE);

echo $result;
echo "</tr>";
echo "</table>";

To send the request to the API, iam using Guzzle And the format the API require is {xx:xx},{xx:xx} without the square bracket, any idea how to remove it using PHP. Thanks in advance

     $client = new Client([
    'headers' => ['Content-Type' => 'application/json',
                  'Token' => $token]

       $response = $client->post('http://localhost:91/Religious',
       ['body' => ***Where the json will be place***]

I read a nice solution in the comments of the first post by deceze ♦ with trim().

$yourJson = trim($yourJson, '[]');

You can also use regular expression:

// if nothing is found, your json has already no brackets or is invalid.
if (preg_match('/^\[(.+)\]$/', $yourJson, $new))
     * $new[0] = $yourJson
     * $new[1] = what's in the parenthesis
    $yourJson = $new[1];

Or, you may use substr():

$yourJson = substr($yourJson, 1, strlen($yourJson) - 2);

EDIT: When it says in the request body format : application/json, I do not think that you have to remove the brackets. Did you even try with them?

You are adding them yourself when you add values to your array in php: $​user_list[] = array($user_count_id=>$row);. If you don't want that, you  Update the question so it's on-topic for Magento Stack Exchange. Closed 2 years ago . I have this Js Obect, I need to trim/remove one set of square braces "the outermost ones [ ]" from the object .

You might need to use the implode function after a json decode. for example:

implode(',',json_decode($text_from_db or $array))

for example $text_from_db=["rice","beans"]

the result will be- rice,beans

Hi folks, I want to pass some PHP arrays to JSON. I am managing to http://​ · http://​ Hello, i want to ask, i maintain a website that using a processwire and php, and i. To remove the square brackets that surround the JSON output of the FOR JSON clause by default, specify the WITHOUT_ARRAY_WRAPPER option. Use this option with a single-row result to generate a single JSON object as output instead of an array with a single element.

Try this

str_replace(array('[', ']'), '', htmlspecialchars(json_encode($result), ENT_NOQUOTES));

I have one json array object and i need to extract one particular value out of it, i used JSONPATH to extract it and the output is coming like this  remove square brackets from json array php (2) . Before reading this post, I had this:

I've added comments as to what the OP is trying to do. But here's my solution for future visitors of this question.

class Thing implements \JsonSerializable
    private $name;
    private $description;
    private $code;

     * Get the value of name
    public function getName()
        return $this->name;

     * Set the value of name
     * @return  self
    public function setName($name)
        $this->name = $name;

        return $this;

     * Get the value of description
    public function getDescription()
        return $this->description;

     * Set the value of description
     * @return  self
    public function setDescription($description)
        $this->description = $description;

        return $this;

     * Get the value of code
    public function getCode()
        return $this->code;

     * Set the value of code
     * @return  self
    public function setCode($code)
        $this->code = $code;

        return $this;

    public function jsonSerialize()
        $vars = get_object_vars($this);

        return $vars;

$thing = new Thing();

$results['items'] = [
    'thing1' => $thing,
    'thing2' => $thing

echo json_encode($results);

Here's the output

    "items": {
        "thing1": {
            "name": "Name",
            "description": "Description",
            "code": "Code"
        "thing2": {
            "name": "Name",
            "description": "Description",
            "code": "Code"

To make square brackets in JSON, which is basically array in JSON you need to write array twice. So make it array(array('something'. Hi, Logstash newbe here and I think it rocks. I'm converting xml files to output JSON but I noticed that in the JSON all values that are not objects themselves are arrays.

you can trimit see example below trim($data, '[]')

I want to use this json in another javascript file, but the absence of square brackets makes it extremely difficult to parse properly or use it the object as it originally  Normal Text Quote Code Header 1 Header 2 Header 3 Header 4 Header 5. Insert link Remove link. Insert Table Add Row Above Add Row Below Add Column Left Add Column Right Add Header Delete Header Delete Column Delete Row Delete Table. Align text to the left Center text Align text to the right Justify text.

change $products[] = to $products = in your for each loop, that should fix the extra square brackets. And if you want to get all entries and their  This feature is not available right now. Please try again later.

How can this be done , I did via PHP before sending it to JS but that didn,t help. share. Share a link to this question. Copy link. improve this question follow. Retrieve data from store when JSON with square bracket [] If this is your first visit, you may have to register before you can post. To start viewing messages, select the forum that you want to visit from the selection below.

7.1.0, An empty JSON key ("") can be encoded to the empty object property instead can be accomplished by encapsulating the element name within braces and the This function will remove trailing commas and encode in utf8, which might  JSON is a simple, text-based way to store and transmit structured data. By using a simple syntax, you can easily store anything from a single number through to strings, arrays, and objects using nothing but a string of plain text. You can also nest arrays and objects, allowing you to create complex data structures.

  • Your desired result is not valid json.
  • Uhm… trim($json, '[]')…? But really, assuming the remote API wants actual JSON, this makes no sense.
  • I am aware of that, it's just the API require format like {xx}:{xx},{xx}:{xx}
  • So it doesn't want JSON, then.
  • If that's the format, then it does want JSON, and you're just confused about how to pass it. It wants a single object; you shouldn't be passing two.
  • yes, i did, it only work when there's no square bracket
  • Using trim () works, thanks, still fails to send the data to the API for some reason
  • @Codezzz: Maybe because you're wrong about the format. Are there docs online for this API you're talking about? Cause it's quite rare to require semi-JSON and call it JSON.
  • @Codezzz: try $client->post('http://localhost:91/Religious', ['json' =>$json ] );. I replaced body by json. The $json variable has to be an array. Seen there :
  • @iArcadia: if you use json, looks like you're expected to pass the actual array, not a JSON string.
  • or you can do it better like this: json_decode(trim($address_pa, '[]'),true);
  • Why Downvote buddy? I have told you to use string operations and trim is one of them :P
  • Not my downvote, but "perform string operations" is devoid of actual details.