Installation
PIP
pip install jsonwriter
Examples
Initialize your file:
If you set autosave to True
every change you make will be automatically saved
from jsonwriter import file
file = file('filename.json', autosave=True)
file.set('key', 'value')
If you don't use autosave you have to add file.save()
whenever you want to save your changes
from jsonwriter import file
file = file('filename.json', autosave=False)
file.set('key', 'value')
file.set('key2', 'value2')
file.save()
Functions
Let's say this is our file content:
{
"name": "Nawaf",
"age": 10
}
get(key)
file.get('name')
file.get('age')
set(key, value)
file.set('Skills', ['Sleeping', 'Coding'], indent=3)
file.set('age', 100)
File will get updated to
{
"name": "Nawaf",
"age": 100,
"Skills": [
"Sleeping",
"Coding"
]
}
If we set the indentation to 0
this is what we will get
{"name": "Nawaf", "age": 100, "Skills": ["Sleeping", "Coding"]}
remove(key)
file.remove('name')
clear()
file.clear()
hasKey(key)
file.hasKey('age')
hasValue(value)
file.hasValue(10)
hasAll(key or value)
file.hasAll('age')
file.hasAll(10)
Attributes
from jsonwriter import file
file = file('filename.json', autosave=True)
print(file.content)
print(file.keys)
print(file.values)