-3

I want to remove the initial brackets from response received from backend data. kindly help to achieve this.

Here I share the server response value.

{ **----------> want to remove this bracket**
"networks": [
{
  "id": "@cred4",
  "isSplit": true,
  "enabled": false,
  "ssid": "Guest5531FC",
  "password": "Admin@123",
  "securityMode": "WPA2_PSK",
  "isGuest": true,
  "visible": true,
  "isBackhaul": false,
  "frequencyBand": "5_GHZ",
  "connectedDevices": 0
},
{
  "id": "@cred0",
  "isSplit": true,
  "enabled": true,
  "ssid": "Office_telcoo",
  "password": "Admn@234",
  "isGuest": false,
  "visible": true,
  "isBackhaul": false,
  "frequencyBand": "2_4_GHZ",
  "connectedDevices": 0
}
]
} **----------> want to remove this bracket**

Kindly help me and thanks.

B.Saravana Kumar
  • 1,208
  • 6
  • 20
  • 48
  • Does this answer your question? [Swift replace substring regex](https://stackoverflow.com/questions/28503449/swift-replace-substring-regex) – Justinas Aug 22 '23 at 10:19
  • 7
    _But_ why would you want to do that? It will create invalid JSON structure – Justinas Aug 22 '23 at 10:19
  • 2
    yes @Justinas is right just put "networks" as Array of objects, ignore "{" – Chandaboy Aug 22 '23 at 10:34
  • 1
    As @Justinas says, what you posted is valid JSON, and by removing those brackets, it would render the JSON invalid. Why do you want to do that? – Duncan C Aug 22 '23 at 12:25

1 Answers1

2

What you posted appears to be valid JSON. Removing the outer braces will make it invalid, so why do you want to do that?

As @hamedHashemi said in their now-deleted post, you could convert your JSON to a string (if it isn't in a string already) and use String.removeFirst()/String.removeLast():

var string =
"""
{
"networks": [
{
  "id": "@cred4",
  "isSplit": true,
  "enabled": false,
  "ssid": "Guest5531FC",
  "password": "Admin@123",
  "securityMode": "WPA2_PSK",
  "isGuest": true,
  "visible": true,
  "isBackhaul": false,
  "frequencyBand": "5_GHZ",
  "connectedDevices": 0
},
{
  "id": "@cred0",
  "isSplit": true,
  "enabled": true,
  "ssid": "Office_telcoo",
  "password": "Admn@234",
  "isGuest": false,
  "visible": true,
  "isBackhaul": false,
  "frequencyBand": "2_4_GHZ",
  "connectedDevices": 0
}
]
}
"""
string.removeFirst()
string.removeLast()
print(string)
Duncan C
  • 128,072
  • 22
  • 173
  • 272