Skip to main content

Lints, formats, and diffs JSON files

Project description

json-lint-tools

Provides linting, diffing and formatting JSON. Diffing and linting is done by comparing an input (file or stdin) to what would be produced by loading the string with json.load(data) then dumping it again with json.dumps(data) with options to order keys, order items in arrays, and specify the expected indentation. The CLI tool can optionally update the original file with the pretty formatted JSON output.

This package is primarily intended as a CLI tool for linting and formatting JSON. Thereis however documentation for the API HERE

Installation

pip install json-lint-tools

CLI Usage

usage: jsonfmt [-h] [--check] [-d] [-f] [-i INDENT] [--no-color] [-r] [-s] [--sort-arrays] [--sort-keys] [--stdin] [path ...]

positional arguments:
  path                  Path to a single file or directory of JSON files

options:
  -h, --help            show this help message and exit
  --check               Only check if the files would be changed. Show a list of any files that would be changed and exit with an error if any would be
  -d, --diff            Show a diff
  -f, --format          Format non-conforming files
  -i INDENT, --indent INDENT
                        How many spaces are expected per indentation
  --no-color            Don't print colors to stdout
  -r, --recursive       Search recursively for JSON files
  -s, --sort            Sort all objects by key and all arrays alphabetically. Shorthand for --sort-keys + --sort-arrays
  --sort-arrays         Sort arrays alphabetically
  --sort-keys           Sort keys in JSON objects alphabetically
  --stdin               Read data from stdin instead of a file

Examples

Getting a pretty diff:

> jsonfmt -r -d tests/jsonfiles                                                                                                                                                                             File: tests/jsonfiles/test.json
---
+++
@@ -6,9 +6,9 @@
   ],
   "baz": {
     "myarry": [
-        "b",
-        "d",
-        "a"
+      "b",
+      "d",
+      "a"
     ]
   }
 }
File: tests/jsonfiles/root_array.json
---
+++
@@ -1 +1,8 @@
-["a", "d", "b", {"foo": "bar"}]
+[
+  "a",
+  "d",
+  "b",
+  {
+    "foo": "bar"
+  }
+]
File: tests/jsonfiles/one_line_obj.json
---
+++
@@ -1 +1,4 @@
-{"foo": "bar", "bar": "baz"}
+{
+  "foo": "bar",
+  "bar": "baz"
+}

Formatting stdin:

> echo '{"foo": "bar"}' | jsonfmt
{
  "foo": "bar"
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_lint_tools-0.1.2.tar.gz (4.9 kB view hashes)

Uploaded Source

Built Distribution

json_lint_tools-0.1.2-py3-none-any.whl (6.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page