Python json.tool() Examples
The following are 6
code examples of json.tool().
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 also want to check out all available functions/classes of the module
json
, or try the search function
.
Example #1
Source File: format_json.py From armory with MIT License | 7 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
Source File: tool.py From Imogen with 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
Source File: gce.py From luci-py with 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
Source File: tool.py From android_universal with 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
Source File: tool.py From Fluid-Designer with 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
Source File: tool.py From Project-New-Reign---Nemesis-Main with 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')