GithubHelp home page GithubHelp logo

Comments (2)

raphaelahrens avatar raphaelahrens commented on July 20, 2024

Hi,

      except:
            print("Your JSON is broken, reloading to spit the error out\n\n")
            data = json.load(filehandle)
            quit()

This is strange, the Exception is caught and then the script tries to parse the file again?

Second by just using quit() it is not possible to check if the script ran successfully or not.
sys.exit with the message from the exception could replace the whole block.

    except json.JSONDecodeError as e:
        sys.exit(f"Your JSON is broken\n {e}")

Thirdly why was tempfile.NamedTemporaryFile not used?
I'm asking, because json.dumps could be replaced with json.dump and all the file handling/writing could be removed.
See below

#!/usr/bin/env python3

import sys
import json
import tempfile
import shutil

filename = sys.argv[1]

with open(filename, 'r') as filehandle:
    try:
        json_data = json.load(filehandle)
    except json.JSONDecodeError as e:
        sys.exit(f"Your JSON is broken\n {e}")
    tmp_path = None
    with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as tmp_file:
        json.dump(json_data, tmp_file.file, indent=2)
        tmp_path = tmp_file.name
    shutil.move(tmp_path, filename)

There is also the option of using jq . --indent 2 "$file" > "$tmp"; mv "$tmp" "$file"

from gsd-tools.

joshbressers avatar joshbressers commented on July 20, 2024

I moved this to the gsd-tools repo

json.tool 3.9 allows you to specify whitespace, and since @raphaelahrens pasted that jq command from let's just suggest that. Humans should not be touching this data (I understand they have to at the moment because we need better tools), I don't want to encourage bad behavior

from gsd-tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.