1

I have a folder with 100 json files and I have to add [ at the beginning and ] at the end of each file .

The file structure is:

{
item
}

However, I need to transform all of them like so:

[{
item
}]

How to do that?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Pelide
  • 468
  • 1
  • 4
  • 19

4 Answers4

1

While I would normally recommend using json.load and json.dump for anything related to JSON, due to your specific requirements the following scrip should suffice:

import os

os.chdir('path to your directory')

for fp in os.listdir('.'):
    if fp.endswith('.json'):
        f = open(fp, 'r+')
        content = f.read()
        f.seek(0)
        f.truncate()
        f.write('[' + content + ']')
        f.close()
Mia
  • 2,466
  • 22
  • 38
0

you can use glob module to parse through all the files. then you can read the contents and then modify that content and write back to the file

from glob import glob

for filename in glob('./json/*.json'):
    f = open(filename, 'r')
    contents = f.read()
    f.close()
    new_contents = f"[{contents}]"
    f = open(filename, 'w')
    f.write(new_contents)
    f.close()
Rishabh Gupta
  • 734
  • 6
  • 10
0
import glob
for fn in glob.glob('/path/*'):
    with open(fn, 'r') as f:
        data = f.read()
    with open(fn, 'w') as f:
        f.write('[' + data + ']')
Błotosmętek
  • 12,717
  • 19
  • 29
0

TL;DR

# One file

$ jq '.=[.]' test.json | sponge test.json

# Many files

find . -type f -name '*.json' -exec sh -c "jq '.=[.]' "{}" | sponge "{}"" \;

Breakdown

Let's take a sample file

$ cat test.json 
{
    "hello":"world"
}
$ jq '.=[.]' test.json 

Above, the dot (.) represents the root node. So I'm taking the root node and putting it inside brackets. So we get:

[
  {
    "hello": "world"
  }
]

Now we need to take the output and put it back into the file

$ jq '.=[.]' test.json | sponge test.json
$ cat test.json
[
  {
    "hello": "world"
  }
]

Next, let's find all the json files where we want to do this

$ find . -type f -name '*.json' 
./test.json

We can iterate over each line of the find command and cat it as as follow:

$ find . -type f -name '*.json' -exec cat {}\;
{
    "hello":"world"
}

or instead we can compose the jq command that we need. The filename is passed to the curly braces between -exec and \;

$ find . -type f -name '*.json' -exec sh -c "jq '.=[.]' "{}" | sponge "{}"" \;

Thanks to zeppelin

taari
  • 954
  • 9
  • 8