Pelide
Pelide

Reputation: 528

How to modify multiple json files

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?

Upvotes: 1

Views: 2134

Answers (4)

taari
taari

Reputation: 1074

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

Upvotes: 0

Błotosmętek
Błotosmętek

Reputation: 12927

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 + ']')

Upvotes: 0

Rishabh Gupta
Rishabh Gupta

Reputation: 824

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()

Upvotes: 0

Mia
Mia

Reputation: 2676

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()

Upvotes: 1

Related Questions