Navigate back to the homepage

How to remove square brackets from json object in javascript?

Infinitbility
JavaScript
May 18th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, we are going to learn the proper way to use an array and also remove brackets from JSON object string, here we will see the first ways to bypass removal brackets if this does not work you will also see how can we remove brackets from a JSON object.

here, we will take three types of examples, if your case from mentioned in the first and the second case then go with their solution else follow the last o third case.

Table content

  1. Use of array index
  2. Use of JSON.parse
  3. Remove brackets from JSON string

Let’s start the today’s topic How to remove square brackets from JSON objects in javascript?

Use of array index

When you have a simple array and you want to remove brackets to use their data then it’s the wrong way you have to use just array index syntax to access objects like the below example.

1// your sample array
2const domains = [
3 {
4 name: "Infinitbility",
5 dob: "26-7-2020",
6 domain: "infinitbility.com",
7 email: "[email protected]",
8 }
9];
10
11// access object values
12console.log(domains[0]);
13
14console.log(domains[0].name)

When you run the above program, you will get the object’s value in the console.

let’s check the output.

javascript, Use of array index example
javascript, Use of array index example

Use of JSON.parse()

When you are in a string that’s why you above solution does not work for you, let’s try the JSON.parse() method, it will convert your string array to an array and you can easily use it.

like the following example.

1// your sample array
2let domains = '[{ "name": "Infinitbility", "dob": "26-7-2020", "domain": "infinitbility.com", "email": "[email protected]"}]';
3
4domains = JSON.parse(domains);
5
6console.log('--------------')
7
8// access object values
9console.log(domains[0]);
10
11console.log('--------------')
12console.log(domains[0].name)

When you run the above program, it will convert JSON string to actual and you can also use it properly.

let’s check the output.

javascript, Use of JSON.parse() example
javascript, Use of JSON.parse() example

Remove brackets from JSON string

If your problem is not solved in the above case, then it’s not a problem you want to just remove brackets from the JSON string, let’s see how we can do it.

here we will use the javascript replace() method with regex exp, it will find brackets from the string and replace them with an empty string.

1// your sample array
2let domains = '[{ "name": "Infinitbility", "dob": "26-7-2020", "domain": "infinitbility.com", "email": "[email protected]"}]';
3
4domains.replace(/[\[\]']+/g,'');
javascript, Remove brackets from JSON string example
javascript, Remove brackets from JSON string example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get all values from json object in javascript?

Example of get all values from json object in javascript with step by step guide.

May 17th, 2022 · 1 min read

How to get first value from json object in javascript?

Example of get first value from json object in javascript with step by step guide.

May 17th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]