Python json.tool() Examples
The following are 6 code examples for showing how to use json.tool(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
json
, or try the search function
.
Example 1
Project: armory Author: twosixlabs File: format_json.py License: MIT License | 6 votes |
def json_tool(filepath, check=False) -> bool: """ Equivalent to json.tool utility, except returns whether changes were made Returns whether changes were made """ with open(filepath) as f: content = f.read() obj = json.loads(content) output = json_dumps_pretty(obj) if output != content: if not check: with open(filepath, "w") as f: f.write(output) return True return False
Example 2
Project: Imogen Author: CedricGuillemet File: tool.py License: MIT License | 6 votes |
def main(): prog = 'python -m json.tool' description = ('A simple command line interface for json module ' 'to validate and pretty-print JSON objects.') parser = argparse.ArgumentParser(prog=prog, description=description) parser.add_argument('infile', nargs='?', type=argparse.FileType(), help='a JSON file to be validated or pretty-printed') parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), help='write the output of infile to outfile') parser.add_argument('--sort-keys', action='store_true', default=False, help='sort the output of dictionaries alphabetically by key') options = parser.parse_args() infile = options.infile or sys.stdin outfile = options.outfile or sys.stdout sort_keys = options.sort_keys with infile: try: obj = json.load(infile) except ValueError as e: raise SystemExit(e) with outfile: json.dump(obj, outfile, sort_keys=sort_keys, indent=4) outfile.write('\n')
Example 3
Project: luci-py Author: luci File: gce.py License: Apache License 2.0 | 6 votes |
def get_metadata_uncached(): """Returns the GCE metadata as a dict. Returns None if not running on GCE or if metadata server is unreachable. Refs: https://cloud.google.com/compute/docs/metadata https://cloud.google.com/compute/docs/machine-types To get the at the command line from a GCE VM, use: curl --silent \ http://metadata.google.internal/computeMetadata/v1/?recursive=true \ -H "Metadata-Flavor: Google" | python -m json.tool | less """ raw = _raw_metadata_request('/computeMetadata/v1/?recursive=true') return json.loads(raw) if raw else None
Example 4
Project: android_universal Author: bkerler File: tool.py License: MIT License | 6 votes |
def main(): prog = 'python -m json.tool' description = ('A simple command line interface for json module ' 'to validate and pretty-print JSON objects.') parser = argparse.ArgumentParser(prog=prog, description=description) parser.add_argument('infile', nargs='?', type=argparse.FileType(), help='a JSON file to be validated or pretty-printed') parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), help='write the output of infile to outfile') parser.add_argument('--sort-keys', action='store_true', default=False, help='sort the output of dictionaries alphabetically by key') options = parser.parse_args() infile = options.infile or sys.stdin outfile = options.outfile or sys.stdout sort_keys = options.sort_keys with infile: try: obj = json.load(infile) except ValueError as e: raise SystemExit(e) with outfile: json.dump(obj, outfile, sort_keys=sort_keys, indent=4) outfile.write('\n')
Example 5
Project: Fluid-Designer Author: Microvellum File: tool.py License: GNU General Public License v3.0 | 5 votes |
def main(): prog = 'python -m json.tool' description = ('A simple command line interface for json module ' 'to validate and pretty-print JSON objects.') parser = argparse.ArgumentParser(prog=prog, description=description) parser.add_argument('infile', nargs='?', type=argparse.FileType(), help='a JSON file to be validated or pretty-printed') parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), help='write the output of infile to outfile') parser.add_argument('--sort-keys', action='store_true', default=False, help='sort the output of dictionaries alphabetically by key') options = parser.parse_args() infile = options.infile or sys.stdin outfile = options.outfile or sys.stdout sort_keys = options.sort_keys with infile: try: if sort_keys: obj = json.load(infile) else: obj = json.load(infile, object_pairs_hook=collections.OrderedDict) except ValueError as e: raise SystemExit(e) with outfile: json.dump(obj, outfile, sort_keys=sort_keys, indent=4) outfile.write('\n')
Example 6
Project: Project-New-Reign---Nemesis-Main Author: ShikyoKira File: tool.py License: GNU General Public License v3.0 | 5 votes |
def main(): prog = 'python -m json.tool' description = ('A simple command line interface for json module ' 'to validate and pretty-print JSON objects.') parser = argparse.ArgumentParser(prog=prog, description=description) parser.add_argument('infile', nargs='?', type=argparse.FileType(), help='a JSON file to be validated or pretty-printed') parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), help='write the output of infile to outfile') parser.add_argument('--sort-keys', action='store_true', default=False, help='sort the output of dictionaries alphabetically by key') options = parser.parse_args() infile = options.infile or sys.stdin outfile = options.outfile or sys.stdout sort_keys = options.sort_keys with infile: try: if sort_keys: obj = json.load(infile) else: obj = json.load(infile, object_pairs_hook=collections.OrderedDict) except ValueError as e: raise SystemExit(e) with outfile: json.dump(obj, outfile, sort_keys=sort_keys, indent=4) outfile.write('\n')