From d62cbd3a035f8c0bc4c7ccce1d3ed8e9905b74f6 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Mon, 23 Jan 2012 20:35:11 +0100 Subject: [PATCH] fmtjson: A tool to format JSON data While debugging an issue recently I had the pleasure of looking at a long, single-line string of JSON data. This simple utility makes it possible to format such data into a readable format. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- Makefile.am | 1 + tools/fmtjson | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100755 tools/fmtjson diff --git a/Makefile.am b/Makefile.am index a3165b5a1..d3cefb749 100644 --- a/Makefile.am +++ b/Makefile.am @@ -550,6 +550,7 @@ python_scripts = \ tools/cfgupgrade12 \ tools/cluster-merge \ tools/confd-client \ + tools/fmtjson \ tools/lvmstrap \ tools/move-instance \ tools/ovfconverter \ diff --git a/tools/fmtjson b/tools/fmtjson new file mode 100755 index 000000000..0bfb114fd --- /dev/null +++ b/tools/fmtjson @@ -0,0 +1,45 @@ +#!/usr/bin/python +# + +# Copyright (C) 2011 Google Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +"""Tool to format JSON data. + +""" + +import sys +import simplejson + + +def main(): + """Main routine. + + """ + if len(sys.argv) > 1: + sys.stderr.write("Read JSON data from standard input and write a" + " formatted version on standard output. There are" + " no options or arguments.\n") + sys.exit(1) + + data = simplejson.load(sys.stdin) + simplejson.dump(data, sys.stdout, indent=2, sort_keys=True) + sys.stdout.write("\n") + + +if __name__ == "__main__": + main() -- GitLab