Hofbr
Hofbr

Reputation: 1010

Python is outputting json with string quotes

I have a model that I'm trying to convert into a json so I can better see how the data is changing (it's very deeply nested and hard to get a feel of even with a debugger).

Code looks something like:

derived_game = GameUnderstanding().derive_gamestates(game=game)
        derived_outfile = derived_game.json()

        with open('derived_game.json', 'w') as outfile:
            json.dump(derived_outfile, outfile)

However, it's outputting with these string quotes, which I think is interfering with my tools that can organized json in a readable way.

"{\"roster\": {\"home\": {\"coaches\": [], \"players\": []}, \"away\": {\"coaches\": [], \"players\": []}, \"officials\": []}, \"current_gamestate\":

Is there some way I should be exporting to a json that would retain better formatting?

Upvotes: 0

Views: 98

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177735

If that's the content of your file, then derived_outfile is already a JSON string, and not a Python dictionary. If that is the case, then this should work:

with open('derived_game.json', 'w') as outfile:
    outfile.write(derived_outfile)

Upvotes: 2

Related Questions