Reputation: 147
I've got a json file which contains different 2 layered lists. After every element of the main list, I add a linebreak so you can easily read the data. But when I load the file and make changes to some of the elements in the lists and dump it back in, the indentation is messed up and you can't read it anymore.So how can I make it so that there's a line break after every element of the first layer of the list like before and not after every single element of the second layer list? Thanks for helping me!
My code:
with open("./world_data.json", "r") as f:
data = json.load(f)
new_data = data[level_name] #Accessing the right variable in the json ("level_1_data/"level_2_data")
new_data[button[1][3][1]][button[1][3][0]] = world.block_number #Making changes to the lists
data[level_name] = new_data
data.update(data)
with open("./world_data.json", "w") as f:
json.dump(data, f, indent=4)
Before dumping (the way I want it to look after dumping):
{
"level_1_data": [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1],
[1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 2, 1]
],
"level_2_data": [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
}
After dumping (I left most of the data out because it would be too long):
{
"level_1_data": [
[
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
]
}
Upvotes: 2
Views: 1173
Reputation: 348
I am not sure if saving the file in your desired format is possible with json library. One dirty trick I used was to assume the json.dumps object as string and perform some str replacement to format the data as desired.
Please try the code below:
dict = data
def format_json(data):
digits = [0,1,2,3,4,5,6,7,8,9]
for i in digits:
i = str(i)
data = data.replace((i+',\n'), (i+',')) # remove \n after digit and comma
data = data.replace(', ', ',') # remove unwanted space
data = data.replace(('\n '+i), i) # remove \n and unwanted space before digit
data = data.replace((i+'\n '), i) #remove unwanted space and \n after digit
data = data.replace(',', ', ') # add space after comma for readability
return data
final = format_json(json.dumps(dict, indent=4))
with open('final.json', 'w') as f:
f.write(final)
# checking if the file is parsable
with open('final.json', 'r') as f:
data = json.load(f)
print(data['level_1_data'])
The json file will be saved as desired and it is also loadable after saving. Please note that the format_json is specific for the data in above requested format.
Output file(final.json):
{
"level_1_data": [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1],
[1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 2, 1]
],
"level_2_data": [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
]
}
Upvotes: 1