Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Last active June 14, 2023 05:13
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save keithweaver/ae3c96086d1c439a49896094b5a59ed0 to your computer and use it in GitHub Desktop.
Save keithweaver/ae3c96086d1c439a49896094b5a59ed0 to your computer and use it in GitHub Desktop.
Save JSON file with Python
import json
def writeToJSONFile(path, fileName, data):
filePathNameWExt = './' + path + '/' + fileName + '.json'
with open(filePathNameWExt, 'w') as fp:
json.dump(data, fp)
# Example
data = {}
data['key'] = 'value'
writeToJSONFile('./','file-name',data)
# './' represents the current directory so the directory save-file.py is in
# 'test' is my file name
@vonmoraes
Copy link

i keep get this error:
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

@Nilesh1506
Copy link

hhufddfjvfv

@businessglitch
Copy link

your path has './' and then you re add './' in
filePathNameWExt = './' + path + '/' + fileName + '.json'

so it should be
filePathNameWExt = path + fileName + '.json'

@naman99lalit
Copy link

Thanks for the help . It worked for me :)

@JeremiasJunior
Copy link

nice work, thanks :)

@ArjixWasTaken
Copy link

ArjixWasTaken commented May 20, 2020

why not use:
import os
os.path.join('.', path, filename + '.json')
This solution is more error proof than yours.

@ROF13ThFloor
Copy link

nice :) thank u

@vishwajiit
Copy link

Nice Job Thank a lot

@malangen
Copy link

Thnk's

@mauro-n
Copy link

mauro-n commented Mar 14, 2022

Great job

@i-vers
Copy link

i-vers commented Jun 4, 2022

what if i wanted to add more data as to what it previously had?

@ArjixWasTaken
Copy link

@i-vers

what if i wanted to add more data as to what it previously had?

use the a file permission instead of w, so it would be

with open("file.txt", "w") as f:
    f.write("Hello ")

with open("file.txt", "a") as f:
    f.write("World!")

file.txt

Hello World!

Well, in the case you are dealing with json, you cant do that, so you would have to first read the file
and then rewrite it from scratch using the already read data

in that case it would be

# Save the file
import json

with open("out.json", "w") as f:
    json.dump(f, [1, 2, 3])

# r+ is for both reading and writing
with open("out.json", "r+") as f:
    existing_data = json.load(f)     #  load   [1, 2, 3]  from the file
    existing_data.extend([4, 5, 6])  #  add    [4, 5, 6]   to the loaded data
    json.dump(f, existing_data)     #   write  [1, 2, 3, 4, 5, 6]  to the file

out.json

[1, 2, 3, 4, 5, 6]

@realsteel1104
Copy link

How to create a JSON file and save it to a specific folder?
I have created a JSON file but it gets saved in my project folder but I want to save it in another folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment