The brackets denote a JSON array, containing one element in your example. In Python, simply pick out the first element of the root array and convert back to JSON.

import json
data = json.loads('[...]')
str = json.dumps(data[0])
Answer from Vortico on Stack Overflow
🌐
Medium
medium.com › @tanaydeshmukh3 › remove-square-bracket-3a7c83f1e7eb
Remove square bracket - Tanay Deshmukh - Medium
September 6, 2022 - Remove square brackets from the start & end position of json file · import json with open(“T:\file.json”) as file: df = json.load(file) df = json.dumps(df).strip(‘[]’).replace(‘},’, ‘}’) df · Json · Python · Json Schema · 2 followers · ·4 following ·
🌐
Stack Overflow
stackoverflow.com › questions › 73972719 › remove-square-brackets-between-json-file-dumps
python - Remove square brackets between JSON file dumps - Stack Overflow
Potentially problem is my lack of grasp of Python data types...? ... with open('filepath', 'a+', encoding='utf-8') as f: while response.text != '[]': json.dump(response.json(), f, ensure_ascii=False, indent=4) page += 1000 response = requests.get(baseurl + f"&skip={page}", headers=headers) How do I get rid of these square brackets between each 1000 records please? ... Consider changing the file format from JSON to JSONL -- then each record goes to a separate line.
🌐
Reddit
reddit.com › r/learnpython › removing brackets from json string?
r/learnpython on Reddit: Removing brackets from JSON string?
June 3, 2019 -

So I have this:

response = urllib.request.urlopen(url)

raw_json = json.loads(response.read()) pinventory = json.dumps(raw_json)

I want remove all square brackets and regular brackets from pinventory. This is the url I'm getting the JSON from. https://steamcommunity.com/profiles/76561198345216182/inventory/json/440/2 It would be way easier to parse without the brackets, because they really mess with RegEx. Is there some way to either remove all the square and regular brackets or replace them with whitespace?

🌐
YouTube
youtube.com › watch
How to Remove Square Brackets from Your JSON File with Python - YouTube
Learn how to effectively manipulate JSON data in Python by removing unwanted square brackets from values, improving your file formatting and data integrity.-...
Published   May 27, 2025
Views   0
Find elsewhere
🌐
Safe Community
community.safe.com › home › forums › fme form › general › how to remove square brackets in a json file?
How to remove square brackets in a json file? - FME Community
April 6, 2023 - Can I position the JsonTemplater before generating the json file or do I have to generate the json file first and then apply the formatting ? ... Even though I prefer @david_r​ answer, another approach would be to use an AttributeTrimmer to remove the leading "[" and trailing "]".
🌐
Reddit
reddit.com › r/learnpython › removing new line inside square brackets in json file
r/learnpython on Reddit: removing new line inside square brackets in json file
November 5, 2022 -

I've noticed that using indent in json.dumps, add new lines inside the lists (inside square brackets)

{
    "mylist":[
        1,
        2,
        3,
    ],
    "who": "me"
}

while i wanted something like this

{
    "mylist": [1,2,3]
    "who": "me"
}

I tried to solve it using regex, but i cant figure it out. I tried to split the problem so

  1. i want to display everything inside the square brackets \[(.*?)\]

  2. i want to capture all new lines (\r\n|\r|\n)

So i thought that writing something like re.sub("\[(\r\n|\r|\n)\]","", myjson) would have worked but nope.

any ideas?

🌐
Stack Overflow
stackoverflow.com › questions › 63602043 › how-to-remove-square-brackets-from-string
python - How to remove square brackets from string? - Stack Overflow
I want to remove the both of the square brackets in the output of my code. ... request2 = requests.get('https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId=1045618&betType=FixedWin', headers={'User-Agent': 'Mozilla/5.0'}) json2 = request2.json() for selection in json2['selections']: for fluc in selection['flucs'][0]: flucs1 = ast.literal_eval(selection['flucs']) flucs2 = flucs1[-2:] flucs3 = [[x[1]] for x in flucs2]
🌐
Stack Overflow
stackoverflow.com › questions › 71846878 › how-to-extract-the-json-data-without-square-brackets-in-python
How to extract the json data without square brackets in python - Stack Overflow
April 12, 2022 - Collections in JSON are surrounded by square brackets. Your first example is not correct syntax; that first open bracket doesn't have a closing one. PLEASE REMEMBER that your data doesn't actually include any square brackets. That's just a list of things, and in Python, we PRINT lists by using square brackets.
Top answer
1 of 2
7

You can load the json into a python object, and then convert the python object to YAML. The other solution is to simply iterate over the dictionaries and format it however you want.

Here's an example of converting it to YAML. It doesn't give you precisely what you want, but it's pretty close. There are lots of ways to customize the output, this is just a quick hack to show the general idea:

import json
import yaml

data = json.loads('''
   {
        "A": {
            "A1": 1,
            "A2": 2
        },
        "B": {
            "B1": {
                "B11": {
                    "B111": 1,
                    "B112": 2
                },
                "B12": {
                    "B121": 1,
                    "B122": 2
                }
            },
            "B2": {
                "B21": [1,2],
                "B22": [1,2]
            }
        },
        "C": "CC"
    }
''')

print yaml.safe_dump(data, allow_unicode=True, default_flow_style=False)

This is the output I get:

A:
  A1: 1
  A2: 2
B:
  B1:
    B11:
      B111: 1
      B112: 2
    B12:
      B121: 1
      B122: 2
  B2:
    B21:
    - 1
    - 2
    B22:
    - 1
    - 2
C: CC
2 of 2
1

And if by chance you want it in your originally specified format, you can overload the pyyaml class structure to customize as desired:

Code:

import yaml
from yaml.emitter import Emitter
from yaml.serializer import Serializer
from yaml.representer import Representer
from yaml.resolver import Resolver

class MyRepresenter(Representer):

    def represent_sequence(self, tag, sequence, flow_style=None):
        value = []
        node = yaml.SequenceNode(tag, value, flow_style=flow_style)
        if self.alias_key is not None:
            self.represented_objects[self.alias_key] = node
        best_style = True
        for item in sequence:
            node_item = self.represent_data(item)
            if not (isinstance(node_item, yaml.ScalarNode) and 
                    not node_item.style):
                best_style = False
            value.append(node_item)
        if best_style:
            node = self.represent_data(
                str(', '.join('%s' % x.value for x in value)))
        if flow_style is None:
            if self.default_flow_style is not None:
                node.flow_style = self.default_flow_style
            else:
                node.flow_style = best_style
        return node

class MyDumper(Emitter, Serializer, MyRepresenter, Resolver):

    def __init__(self, stream,
            default_style=None, default_flow_style=None,
            canonical=None, indent=None, width=None,
            allow_unicode=None, line_break=None,
            encoding=None, explicit_start=None, explicit_end=None,
            version=None, tags=None):
        Emitter.__init__(self, stream, canonical=canonical,
                indent=indent, width=width,
                allow_unicode=allow_unicode, line_break=line_break)
        Serializer.__init__(self, encoding=encoding,
                explicit_start=explicit_start, explicit_end=explicit_end,
                version=version, tags=tags)
        MyRepresenter.__init__(self, default_style=default_style,
                default_flow_style=default_flow_style)
        Resolver.__init__(self)

print yaml.dump(data, Dumper=MyDumper, default_flow_style=False)

Produces:

A:
  A1: 1
  A2: 2
B:
  B1:
    B11:
      B111: 1
      B112: 2
    B12:
      B121: 1
      B122: 2
  B2:
    B21: 1, 2
    B22: 1, 2
C: CC