NAME

jsonfmt - format unformatted JSON data

SYNOPSIS

  jsonfmt [options] <jsonfile>

DESCRIPTION

jsonfmt formats a collapsed set of text data into an easy-to-read, expanded version of the data. The data is assumed to be JSON-like, and with the whole thing on a single line. In particular, jsonfmt was written to handle JSON output returned from ElasticSearch queries.

JSON files are not checked for validity and proper formatting. jsonfmt assumes the input is correct and parses it accordingly. If invalid JSON is given (e.g., improper quoting or unbalanced brackets), the jsonfmt may report a problem, but it may also be able to parse the input as valid.

These JSON-like files use a method of grouping data consisting of numbers, text, booleans, lists, and dictionaries. The underlying format is to provide the data as a set of key/value pairs, and the pairs are grouped as required. Some programs, such as ElasticSearch and bro/zeek, sometimes display data in this format, but it is all collapsed into a single line. jsonfmt will take the collapsed form of this data and format it into an easy-to-read form.

The file given on the command line will be formatted. Standard input is read for data if a file is not specified.

An example of the joined-up, hard-to-read version is:

    {u'took': 2, u'timed_out': False}
The formatted version of this example is:

    {
            u'took'     : 2,
            u'timed_out': False
    }
This is easy enough to read in a simple example such as this, but it gets much more difficult when there are 20 or 80 key/value pairs, including lists and additional dictionaries.

This data representation is quite useful, but it does have potential problems. In this format, commas mark the ends of key/value lines but not all commas mark the ends of lines and not all lines are ended with commas. Values aren't quoted and so checks must be performed to see what follows any comma in a value.

The formatting actions are:

For example, jsonfmt will convert this blob of unformatted grouped text:

    [the-answer-to-LUnE=42, foo=84, dogs=[main_dog=woofers, backup_dog=fido, emergency_backup_dog=fifi], bar=888]
into this nicely formatted text:

    [
            the-answer-to-LUnE = 42,
            foo                = 84,
            dogs               = 
            [
                    main_dog             = woofers,
    	            backup_dog           = fido,
    	            emergency_backup_dog = fifi
            ],

    ]
By default, each indentation level is represented by a single tab character. The -indent option allows this to be changed to a particular number of spaces.

Also, the full structure of the JSON file will be displayed. The -depth option can be used to limit the depth of lines that will be shown.

OPTIONS

jsonfmt takes the following options:

SEE ALSO

ElasticSearch, bro(1), zeek(1)

AUTHOR

Wayne Morrison, wayne@waynemorrison.com

LICENSE

Copyright 2020 Wayne Morrison

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.