Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove Square bracket from JSON

I have one json string like below

[

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

]

I want to delete/remove the first and last square brackets from string..So how would I remove the first and last square brackets from the above string using javascript.

Please help me

like image 972
Piyush Avatar asked Oct 31 '13 06:10

Piyush


People also ask

How do you remove square brackets in JSON data?

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.

How do you remove square brackets from JSON response in Python?

In Python, simply pick out the first element of the root array and convert back to JSON.

What is square bracket in JSON?

JSON syntax is basically considered as a subset of JavaScript syntax; it includes the following − Data is represented in name/value pairs. Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by , (comma). Square brackets hold arrays and values are separated by ,(comma).

Why does JSON start with square brackets?

From what I can read on json.org, all JSON strings should start with { (curly brace), and [ characters (square brackets) represent an array element in JSON.


2 Answers

Use this when you return:

return properties[0];

Or

var data = [

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

]; // Or whatever the Json is
data = data[0];

Or if you're accessing the json via another object

var data = jsonObj[0];
like image 99
Linga Avatar answered Oct 26 '22 18:10

Linga


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

]';

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

See this codepen example

like image 34
Jacques Snyman Avatar answered Oct 26 '22 18:10

Jacques Snyman