Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting json cleanly
#1
Daring_T here,
I have a dictionary that appends to a json file, and it outputs on one line.
Is there a way to cleanly format the json data like pprint.
Output:
{"11-19-20_9:18.31": { "a": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0], "b": [2, 2, 2, 0, 0, 0, 0, 0, 0, 0], "c": [3, 3, 3, 0, 0, 0, 0, 0, 0, 0], "d": [4, 4, 4, 4, 0, 0, 0, 0, 0, 0], "e": [5, 5, 5, 5, 5, 0, 0, 0, 0, 0], "g": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "h": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "i": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "j": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}, "11-19-20_17:20.5": { "a": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0], "b": [2, 2, 2, 0, 0, 0, 0, 0, 0, 0], "c": [3, 3, 3, 0, 0, 0, 0, 0, 0, 0], "d": [4, 4, 4, 4, 0, 0, 0, 0, 0, 0], "e": [5, 5, 5, 5, 5, 0, 0, 0, 0, 0], "g": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "h": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "i": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "j": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}}
This is the original json data
Output:
{"11-19-20_9:18.31": {"a": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0], "b": [2, 2, 2, 0, 0, 0, 0, 0, 0, 0], "c": [3, 3, 3, 0, 0, 0, 0, 0, 0, 0], "d": [4, 4, 4, 4, 0, 0, 0, 0, 0, 0], "e": [5, 5, 5, 5, 5, 0, 0, 0, 0, 0], "g": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "h": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "i": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "j": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}, "11-19-20_17:20.5": {"a": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0], "b": [2, 2, 2, 0, 0, 0, 0, 0, 0, 0], "c": [3, 3, 3, 0, 0, 0, 0, 0, 0, 0], "d": [4, 4, 4, 4, 0, 0, 0, 0, 0, 0], "e": [5, 5, 5, 5, 5, 0, 0, 0, 0, 0], "g": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "h": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "i": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "j": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}}
Here the code I have to add dicts to the json file.
import os
import pprint
from datetime import datetime

def output_board_layout(input_file="battleship.json", add_to_key="_in_book"):
    """
    :parameter
        input_file
            inputs json_data to file
            default is battleship.json
        add_to_key
            adds a string to the key
    :return: dict formatted to json
    """
    battleship_grid = {
                "a": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                "b": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                "c": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                "d": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                "e": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                "g": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                "h": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                "i": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                "j": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}

    dt = datetime.today()
    key = f"{str(dt.month)}-{str(dt.day)}-{str(dt.year)[2:]}_{str(dt.hour)}:{str(dt.minute)}.{str(dt.second)}"
    key = key + add_to_key

    new_battleship_grid = dict()
    new_battleship_grid[key] = battleship_grid

    if os.stat(input_file).st_size != 0:
        # Checks if file is not empty
        with open(input_file, "r") as f:
            existing_json_data = json.load(f)

        existing_json_data.update(new_battleship_grid)
        print("\n" * 3)
        pprint.pprint(existing_json_data)

        json_file_data = json.dumps(existing_json_data)
    else:
        json_file_data = json.dumps(new_battleship_grid)

    with open(input_file, "w") as f:
        # json.dump(json_file_data, f)
        f.write(json_file_data)
    return json_file_data
Thanks For reading,
Daring_T
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Impossible to cleanly uninstall Python 3.8.9 Ftaniere 2 1,646 Sep-01-2022, 10:38 AM
Last Post: snippsat
  Problems Formatting JSON primowalker 1 2,445 May-05-2018, 08:28 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020