jsonfmt


Namejsonfmt JSON
Version 0.2.7 PyPI version JSON
download
home_pageNone
SummaryA powerful tool for pretty-printing, querying and conversion JSON documents.
upload_time2024-04-10 07:18:53
maintainerNone
docs_urlNone
authorNone
requires_python>=3.6
licenseMIT License
keywords json formatter pretty-print highlight jmespath
VCS
bugtrack_url
requirements jmespath jsonpath-ng Pygments pyperclip pyyaml toml
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">
  <img src="logo.svg" width="150">
  <h1>𝑱𝒔𝒐𝒏𝑭𝒎𝒕</h1>
</div>


<div align="center">

[![Build Status](https://github.com/seamile/jsonfmt/actions/workflows/python-package.yml/badge.svg)](https://github.com/seamile/jsonfmt/actions)
[![PyPI Version](https://img.shields.io/pypi/v/jsonfmt?color=blue&label=Version&logo=python&logoColor=white)](https://pypi.org/project/jsonfmt/)
[![Installs](https://static.pepy.tech/personalized-badge/jsonfmt?period=total&units=international_system&left_color=grey&right_color=blue&left_text=Installs)](https://pepy.tech/project/jsonfmt)
[![Code Grade](https://app.codacy.com/project/badge/Grade/1e12e3cd8c8342bca68db4caf5b6a31d)](https://app.codacy.com/gh/seamile/jsonfmt/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[![Test Coverage](https://app.codacy.com/project/badge/Coverage/1e12e3cd8c8342bca68db4caf5b6a31d)](https://app.codacy.com/gh/seamile/jsonfmt/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)

</div>

### <div align="center"><a href="README_CN.md">🇨🇳 中文版</a></div>

**_jsonfmt_** (JSON Formatter) is a simple yet powerful JSON processing tool.

As we all know, Python has a built-in tool for formatting JSON data: `python -m json.tool`. However, its functionality is too simple, so **jsonfmt** extends it with many practical features:

🎨 It can not only print JSON data in a pretty way,

🔄 But also convert JSON, TOML, XML and YAML data formats to each other,

🔎 And even extract content from JSON data using JMESPATH or JSONPATH.

🧐 You can even use **jsonfmt** to compare differences between two JSON or other formatted data.


- [Quick Start](#quick-start)
    - [Installation](#installation)
    - [Usage](#usage)
- [User Guide](#user-guide)
    - [1. Pretty Print JSON Data](#1-pretty-print-json-data)
    - [2. Minimize the JSON data](#2-minimize-the-json-data)
    - [3. Extract Partial Content from JSON Data](#3-extract-partial-content-from-json-data)
    - [4. Format Conversion](#4-format-conversion)
    - [5. Diff Comparison](#5-diff-comparison)
    - [6. Handle Large JSON Data Conveniently](#6-handle-large-json-data-conveniently)
    - [7. Modify Values in Input Data](#7-modify-values-in-input-data)
    - [8. Output to File](#8-output-to-file)
- [TODO](#todo)


## Quick Start

### Installation

```shell
$ pip install jsonfmt
```

### Usage

1. Process data from files.

  ```shell
  $ jf [options] [data_files ...]
  ```

2. Process data from `stdin`.

  ```shell
  $ echo '{"hello": "world"}' | jf [options]
  ```

**Positional Arguments**

`files`: The data files to process, supporting JSON / TOML / XML / YAML formats.

**Options**

- `-h`: Show this help documentation and exit.
- `-C`: CopyMode, which will copy the processing result to the clipboard.
- `-d`: DiffMode, which compares the difference between the two input data.
- `-D DIFFTOOL`: DifftoolMode, similar to "DiffMode". You can specify a tool to perform diff comparisons.
- `-o`: OverviewMode, which can display an overview of the structure of the data, helping to quickly understand larger data.
- `-O`: OverwriteMode, which will overwrite the original file with the formated text.
- `-c`: Suppress all whitespace separation (most compact), only valid for JSON.
- `-e`: Escape all characters to ASCII codes.
- `-f`: The format to output (default: same as input data format, options: `json` / `toml` / `xml` / `yaml`).
- `-i`: Number of spaces for indentation (default: 2, range: 0~8, set to 't' to use <kbd>Tab</kbd> as indentation).
- `-l`: Query language for extracting data (default: auto-detect, options: jmespath / jsonpath).
- `-p QUERYPATH`: JMESPath or JSONPath query path.
- `-s`: Sort the output of dictionaries alphabetically by key.
- `--set 'foo.k1=v1;k2[i]=v2'`: Key-value pairs to add or modify (separated by ";").
- `--pop 'k1;foo.k2;k3[i]'`: Key-value pairs to delete (separated by ";").
- `-v`: Show the version.


## User Guide

In order to demonstrate the features of jsonfmt, we need to first create a test data and save it to the file example.json. The file contents are as follows:

```json
{
    "name": "Bob",
    "age": 23,
    "gender": "纯爷们",
    "money": 3.1415926,
    "actions": [
        {
            "name": "eating",
            "calorie": 1294.9,
            "date": "2021-03-02"
        },
        {
            "name": "sporting",
            "calorie": -2375,
            "date": "2023-04-27"
        },
        {
            "name": "sleeping",
            "calorie": -420.5,
            "date": "2023-05-15"
        }
    ]
}
```

Then, convert this data to TOML, XML and YAML formats, and save them as example.toml, example.xml and example.yaml respectively.

These data files can be found in the *test* folder of the source code:

```
test/
|- example.json
|- example.toml
|- example.xml
|- example.yaml
```

### 1. Pretty Print JSON Data

#### Syntax Highlighting and Indentation

The default working mode of jsonfmt is to format the data and print it with syntax highlighting.

The option `-i` specifies the number of spaces for indentation. By default, it is 2 spaces, and the number of spaces allowed is between 0 and 8. If you want to use the <kbd>tab</kbd> as indentation, set it to `t`.

The `-s` option is used to sort the dictionary alphabetically by key.

If there are some non-ASCII characters in the JSON data, you can use `-e` to escape them.

```shell
$ jf -s -i 4 test/example.json
```

Output:

```json
{
    "actions": [
        {
            "calorie": 1294.9,
            "date": "2021-03-02",
            "name": "eating"
        },
        {
            "calorie": -2375,
            "date": "2023-04-27",
            "name": "sporting"
        },
        {
            "calorie": -420.5,
            "date": "2023-05-15",
            "name": "sleeping"
        }
    ],
    "age": 23,
    "gender": "纯爷们",
    "money": 3.1415926,
    "name": "Bob"
}
```

#### Read JSON from Pipeline

Sometimes the data to be processed comes from the output of other commands. Just use the pipe character `|` to connect the two commands and then take it from the `stdin`.

```shell
$ curl -s https://jsonplaceholder.typicode.com/posts/1 | jf -i 4
```

Output:

```json
{
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nrep..."
}

```

### 2. Minimize the JSON data

The `-c` option used to suppress all whitespace and newlines to compact the JSON data into a single line.

```shell
$ echo '{
    "name": "alex",
    "age": 21,
    "items": [
        "pen",
        "phone"
    ]
}' | jf -c
```

Output:

```json
{"name":"alex","age":21,"items":["pen","phone"]}
```

### 3. Extract Partial Content from JSON Data

jsonfmt uses both [**JMESPath**](https://jmespath.org/) and [**JSONPath**](https://datatracker.ietf.org/doc/id/draft-goessner-dispatch-jsonpath-00.html) as its query languages.

**JMESPath** (JSON Meta Language for Expression Path) is a query language introduced by AWS for processing JSON data. Among the many JSON query languages, JMESPath seems to be the most widely-used, fastest-growing, and highest-rated. Its syntax is more concise and more universal than [**jq**](https://jqlang.github.io/jq/), and its functionality is more powerful and feature-rich than JSONPath. Therefore, I prefer to use it as the primary JSON query language.

JMESPath can elegantly use simple syntax to extract part of the content from JSON data, and also compose the filtered data into a new object or array. The official JMESPath tutorial is [here](https://jmespath.org/tutorial.html).

#### JMESPath Examples

- Extract the first item of `actions` from *example.json*:

    ```shell
    $ jf -p 'actions[0]' test/example.json
    ```

    Output:

    ```json
    {
        "name": "eating",
        "calorie": 1294.9,
        "date": "2021-03-02"
    }
    ```

- Filter all items with `calorie < 0` from `actions`.

    ```shell
    # Here, `0` means 0 is a number
    $ jf -p 'actions[?calorie<`0`]' test/example.json
    ```

    Output:

    ```json
    [
        {
            "name": "sporting",
            "calorie": -2375,
            "date": "2023-04-27"
        },
        {
            "name": "sleeping",
            "calorie": -420.5,
            "date": "2023-05-15"
        }
    ]
    ```

- Show all keys and the length of `actions`.

    ```shell
    $ jf -p '{all_keys:keys(@), actions_len:length(actions)}' test/example.json
    ```

    Output:

    ```json
    {
        "all_keys": [
            "name",
            "age",
            "gender",
            "money",
            "actions"
        ],
        "actions_len": 3
    }
    ```

- Sort the items in `actions` by their `calorie` value, and define the result as a new dictionary.

    ```shell
    $ jf -p 'sort_by(actions, &calorie)[].{foo: name, bar:calorie}' test/example.json
    ```

    Output:

    ```json
    [
        {
            "foo": "sporting",
            "bar": -2375
        },
        {
            "foo": "sleeping",
            "bar": -420.5
        },
        {
            "foo": "eating",
            "bar": 1294.9
        }
    ]
    ```

[More JMESPath examples](https://jmespath.org/examples.html).

#### JSONPath Examples

JSONPath was inspired by the design of XPath. Therefore, it can precisely locate any element in the JSON document through path expressions, similar to XPath, enabling efficient retrieval, filtering, and operation of complex nested data.

Unlike the tag hierarchical structure of XML, JSONPath specially handles JSON key-value pairs and arrays, allowing users to conveniently access multi-level object properties, iterate over objects and arrays, and filter data based on conditions.

Some queries that are difficult to handle with JMESPath can be easily achieved with JSONPath.

- Filter all `name` fields using relative paths:

    ```shell
    # Use -l to specify the query language as JSONPath
    $ jf -l jsonpath -p '$..name' test/example.json
    ```

    Output:

    ```json
    [
        "Bob",
        "eating",
        "sporting",
        "sleeping"
    ]
    ```

#### Querying TOML, XML and YAML

One of the powerful features of jsonfmt is that you can process TOML, XML and YAML in exactly the same way as JSON, and freely convert the result format. You can even process these four formats simultaneously in one command.

- Read data from a toml file and output in YAML format

    ```shell
    $ jf -p '{all_keys:keys(@), actions_len:length(actions)}' test/example.toml -f yaml
    ```

    Output:

    ```yaml
    all_keys:
    - name
    - age
    - gender
    - money
    - actions
    actions_len: 3
    ```

- Process three formats at once

    ```shell
    $ jf -p 'actions[0]' test/example.json test/example.toml test/example.yaml
    ```

    Output:

    ```yaml
    1. test/example.json
    {
        "name": "eating",
        "calorie": 1294.9,
        "date": "2021-03-02"
    }

    2. test/example.toml
    name = "eating"
    calorie = 1294.9
    date = "2021-03-02"

    3. test/example.xml
    <?xml version="1.0" ?>
    <root>
        <name>eating</name>
        <calorie>1294.9</calorie>
        <date>2021-03-02</date>
    </root>

    4. test/example.yaml
    name: eating
    calorie: 1294.9
    date: '2021-03-02'
    ```


### 4. Format Conversion

*jsonfmt* supports processing JSON, TOML, XML and YAML formats. Each format can be converted to other formats by specifying the "-f" option.

<div style="color: orange"><strong>Note:</strong></div>

1. `null` is not supported in TOML. Therefore, all `null` values will be deleted when converting from other formats to TOML.

2. XML does not support multi-dimensional arrays. Therefore, if the original data contains multi-dimensional arrays, a wrong data will be generated during the conversion to XML format.

#### Example 1. JSON to YAML

```shell
$ jf test/example.json -f yaml
```

Output:

```yaml
name: Bob
age: 23
gender: 纯爷们
money: 3.1415926
actions:
- name: eating
  calorie: 1294.9
  date: '2021-03-02'
- name: sporting
  calorie: -2375
  date: '2023-04-27'
- name: sleeping
  calorie: -420.5
  date: '2023-05-15'
```

#### Example 2. TOML to XML

```shell
$ jf test/example.toml -f xml
```

Output:

```xml
<?xml version="1.0" ?>
<root>
    <name>Bob</name>
    <age>23</age>
    <gender>纯爷们</gender>
    <money>3.1415926</money>
    <actions>
        <name>eating</name>
        <calorie>1294.9</calorie>
        <date>2021-03-02</date>
    </actions>
    <actions>
        <name>sporting</name>
        <calorie>-2375</calorie>
        <date>2023-04-27</date>
    </actions>
    <actions>
        <name>sleeping</name>
        <calorie>-420.5</calorie>
        <date>2023-05-15</date>
    </actions>
</root>
```


### 5. Diff Comparison

In development, we often need to compare differences between some data or configurations. For example, compare the return results of an API when passing in different parameters, or compare the differences between system configuration files in different formats by operations personnel.

jsonfmt supports various diff-tools by default, such as `diff`, `vimdiff`, `git`, `code`, `kdiff3`, `meld`, and also supports `WinMerge` and `fc` on Windows, and other tools can also be supported through the `-D` option.

By default, jsonfmt will first check if git is installed on the computer. If git is available, jsonfmt will call `git config --global diff.tool` to read the configured diff-tool. If it's not set, it will use the default diff-tool of git for processing. If git is not available, it will search in the order of `code`, `kdiff3`, `meld`, `vimdiff`, `diff`, `WinMerge`, `fc`. If no available diff-tool is found, jsonfmt will exit with an error.

In DiffMode, jsonfmt will first format the data to be compared (at this time, the `-s` option will be automatically enabled), and save the result to a temporary file, and then call the specified tool for diff comparison.

#### Example 1: Compare two JSON files

```shell
$ jf -d test/example.json test/another.json
```

Output:

```diff
--- /tmp/.../jf-jjn86s7r_example.json     2024-03-23 18:22:00
+++ /tmp/.../jf-vik3bqsu_another.json     2024-03-23 18:22:00
@@ -3,21 +3,16 @@
     {
       "calorie": 1294.9,
       "date": "2021-03-02",
-      "name": "eating"
+      "name": "thinking"
     },
     {
-      "calorie": -2375,
-      "date": "2023-04-27",
-      "name": "sporting"
-    },
-    {
       "calorie": -420.5,
       "date": "2023-05-15",
       "name": "sleeping"
     }
   ],
   "age": 23,
-  "gender": "纯爷们",
+  "gender": "male",
   "money": 3.1415926,
-  "name": "Bob"
+  "name": "Tom"
 }
```

#### Example 2: Specify diff-tool with `-D`

The `-D DIFFTOOL` option can specify a diff comparison tool. As long as its command format matches `command [options] file1 file2`, it doesn't matter whether it's in jsonfmt's default supported tool list or not.

```shell
$ jf -D sdiff test/example.json test/another.json
```

Output:

```
{                                   {
  "actions": [                        "actions": [
    {                                   {
      "calorie": 1294.9,                  "calorie": 1294.9,
      "date": "2021-03-02",               "date": "2021-03-02",
      "name": "eating"         |          "name": "thinking"
    },                                  },
    {                                   {
      "calorie": -2375,        <
      "date": "2023-04-27",    <
      "name": "sporting"       <
    },                         <
    {                          <
      "calorie": -420.5,                  "calorie": -420.5,
      "date": "2023-05-15",               "date": "2023-05-15",
      "name": "sleeping"                  "name": "sleeping"
    }                                   }
  ],                                  ],
  "age": 23,                          "age": 23,
  "gender": "纯爷们",          |      "gender": "male",
  "money": 3.1415926,                 "money": 3.1415926,
  "name": "Bob"                |      "name": "Tom"
}                                   }
```

#### Example 3: Specify options for the selected tool

If you need to pass parameters to the diff-tool, you can use `-D 'DIFFTOOL OPTIONS'`.

```shell
$ jf -D 'diff --ignore-case --color=always' test/example.json test/another.json
```

Output:

```diff
6c6
<       "name": "eating"
---
>       "name": "thinking"
9,13d8
<       "calorie": -2375,
<       "date": "2023-04-27",
<       "name": "sporting"
<     },
<     {
20c15
<   "gender": "纯爷们",
---
>   "gender": "male",
22c17
<   "name": "Bob"
---
>   "name": "Tom"
```

#### Example 4: Compare data in different formats

For data from different sources, their formats, indentation, and key order may be different. In this case, you can use `-i` and `-f` together for diff comparison.

```shell
$ jf -d -i 4 -f toml test/example.toml test/another.json
```

Output:

```diff
--- /var/.../jf-qw9vm33n_example.toml     2024-03-23 18:29:17
+++ /var/.../jf-dqb_fl4x_another.json     2024-03-23 18:29:17
@@ -1,18 +1,13 @@
 age = 23
-gender = "纯爷们"
+gender = "male"
 money = 3.1415926
-name = "Bob"
+name = "Tom"
 [[actions]]
 calorie = 1294.9
 date = "2021-03-02"
-name = "eating"
+name = "thinking"

 [[actions]]
-calorie = -2375
-date = "2023-04-27"
-name = "sporting"
-
-[[actions]]
 calorie = -420.5
 date = "2023-05-15"
 name = "sleeping"
```

### 6. Handle Large JSON Data Conveniently

Very often, JSON data from program interfaces is very large, which makes it difficult for us to read, debug, and process. jsonfmt provides four ways to handle large JSON data:

- Use JMESPath or JSONPath to read part of the content ([already covered in the previous section](#3-extract-partial-content-from-json-data))
- [Use pager mode to view larger JSON data](#use-pager-mode-to-view-larger-json-data)
- [Show an overview of large JSON data](#show-an-overview-of-large-json-data)
- [Copy the processing result to the clipboard](#copy-the-processing-result-to-the-clipboard)

#### Use pager mode to view larger JSON data

Pager mode is similar to the `more` command. When the JSON data is too large to be fully displayed in the window area, *jsonfmt* will automatically display the result in pager mode.

The operations in pager mode are the same as the `more` command:

| Key                                           | Description                    |
|-----------------------------------------------|--------------------------------|
| <kbd>j</kbd>                                  | Move forward one line          |
| <kbd>k</kbd>                                  | Move backward one line         |
| <kbd>f</kbd> or <kbd>ctrl</kbd>+<kbd>f</kbd>  | Move forward one page          |
| <kbd>b</kbd>  or <kbd>ctrl</kbd>+<kbd>b</kbd> | Move backward one page         |
| <kbd>g</kbd>                                  | Jump to the top of the page    |
| <kbd>G</kbd>                                  | Jump to the bottom of the page |
| <kbd>/</kbd>                                  | Search mode                    |
| <kbd>q</kbd>                                  | Exit pager mode                |

The return value of this API below is a large JSON data, you can paste this command into the terminal to try the pager mode:

```shell
$ curl -s https://jsonplaceholder.typicode.com/users | jf
```

#### Show an overview of large JSON data

Sometimes we only want to see an overview of the JSON data without caring about the details. In this case, you can use the `-o` option. It will clear the sublists in the JSON, and replace the strings to `"..."` to show the overview.

If the root node of the JSON data is a list, only its first child element will be preserved in the overview.

```shell
$ jf -o test/example.json
```

Output:

```json
{
    "name": "...",
    "age": 23,
    "gender": "...",
    "money": 3.1415926,
    "actions": []
}
```

#### Copy the processing result to the clipboard

If you want to paste the processed result into a file, but the output printed in the terminal exceeds one page, it may be difficult to copy. In this case, you can use the `-C` option to automatically copy the result to the clipboard.

```shell
$ jf -C test/example.json
```

After completing the above operation, you can use <kbd>ctrl</kbd>+<kbd>v</kbd> or <kbd>cmd</kbd>+<kbd>v</kbd> to paste the result into other documents.

<div style="color: orange"><strong>Note:</strong></div>

When processing multiple targets at the same time, such as: `jf -C file1 file2 file3 ...`, jsonfmt will copy the processing results of all files to the clipboard, with two newline characters `'\n\n'` separating multiple results.

### 7. Modify Values in Input Data

When you need to change some content in the input document, use the `--set` and `--pop` options.

The format is `--set 'key=value'`. If you need to modify multiple values, you can separate them with `;`, like this: `--set 'k1=v1;k2=v2'`. If the key-value pair does not exist, it will be added.

For items in a list, use `key[i]` or `key.i` to specify. If the index is greater or equal to the number of elements, the value will be appended.

#### Add key-value pairs

```shell
# Add country = China, and append an item to actions
$ jf --set 'country=China; actions[3]={"name": "drinking"}' test/example.json
```

Output:

```json
{
    "name": "Bob",
    "age": 23,
    "gender": "纯爷们",
    "money": 3.1415926,
    "actions": [
        {
            "name": "eating",
            "calorie": 1294.9,
            "date": "2021-03-02"
        },
        {
            "name": "sporting",
            "calorie": -2375,
            "date": "2023-04-27"
        },
        {
            "name": "sleeping",
            "calorie": -420.5,
            "date": "2023-05-15"
        },
        {
            "name": "drinking"
        }
    ],
    "country": "China"
}
```

#### Modify values

```shell
# Modify money and actions[1]["name"]
$ jf --set 'money=1000; actions[1].name=swim' test/example.json
```

Output:

```json
{
    "name": "Bob",
    "age": 23,
    "gender": "纯爷们",
    "money": 1000,
    "actions": [
        {
            "name": "eating",
            "calorie": 1294.9,
            "date": "2021-03-02"
        },
        {
            "name": "swim",
            "calorie": -2375,
            "date": "2023-04-27"
        },
        {
            "name": "sleeping",
            "calorie": -420.5,
            "date": "2023-05-15"
        }
    ]
}
```

#### Delete key-value pairs

```shell
# Delete gender and actions[1]
$ jf --pop 'gender; actions[1]' test/example.json
```

Output:

```json
{
    "name": "Bob",
    "age": 23,
    "money": 3.1415926,
    "actions": [
        {
            "name": "eating",
            "calorie": 1294.9,
            "date": "2021-03-02"
        },
        {
            "name": "sleeping",
            "calorie": -420.5,
            "date": "2023-05-15"
        }
    ]
}
```

Of course, you can also use `--set` and `--pop` at the same time:

```shell
jf --set 'skills=["Django","Flask"];money=1000' --pop 'gender;actions[1]' test/example.json
```

<div style="color: orange"><strong>Note:</strong></div>
The above command will not modify the original JSON file. If you want to do so, see below.

### 8. Output to File

jsonfmt does not provide a dedicated option to write the processing result to a file. Because you can easily handle this by using the terminal's redirection symbol `>`, which is supported on both Linux and Windows.

```shell
$ jf -si 4 test/example.json > formatted.json
```

If you need to overwrite the processed result to the original file, you can use the `-O` option:

```shell
# Sort by object keys, set indentation to 4 spaces, set the name value to Alex, and write the final result to the original file
$ jf -s -i 4 --set 'name=Alex' -O test/example.json
```

## TODO

- [ ] Add URL support to directly compare data from two APIs
- [ ] Add INI format support
- [ ] Add merge mode to combine multiple JSON or other formatted data into one

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "jsonfmt",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "json, formatter, pretty-print, highlight, jmespath",
    "author": null,
    "author_email": "Seamile <lanhuermao@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/be/c2/c31d1fad0757bd9747c51eb0686634a34745d3c93f812d5b87b2558074dc/jsonfmt-0.2.7.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n  <img src=\"logo.svg\" width=\"150\">\n  <h1>\ud835\udc71\ud835\udc94\ud835\udc90\ud835\udc8f\ud835\udc6d\ud835\udc8e\ud835\udc95</h1>\n</div>\n\n\n<div align=\"center\">\n\n[![Build Status](https://github.com/seamile/jsonfmt/actions/workflows/python-package.yml/badge.svg)](https://github.com/seamile/jsonfmt/actions)\n[![PyPI Version](https://img.shields.io/pypi/v/jsonfmt?color=blue&label=Version&logo=python&logoColor=white)](https://pypi.org/project/jsonfmt/)\n[![Installs](https://static.pepy.tech/personalized-badge/jsonfmt?period=total&units=international_system&left_color=grey&right_color=blue&left_text=Installs)](https://pepy.tech/project/jsonfmt)\n[![Code Grade](https://app.codacy.com/project/badge/Grade/1e12e3cd8c8342bca68db4caf5b6a31d)](https://app.codacy.com/gh/seamile/jsonfmt/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)\n[![Test Coverage](https://app.codacy.com/project/badge/Coverage/1e12e3cd8c8342bca68db4caf5b6a31d)](https://app.codacy.com/gh/seamile/jsonfmt/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)\n\n</div>\n\n### <div align=\"center\"><a href=\"README_CN.md\">\ud83c\udde8\ud83c\uddf3 \u4e2d\u6587\u7248</a></div>\n\n**_jsonfmt_** (JSON Formatter) is a simple yet powerful JSON processing tool.\n\nAs we all know, Python has a built-in tool for formatting JSON data: `python -m json.tool`. However, its functionality is too simple, so **jsonfmt** extends it with many practical features:\n\n\ud83c\udfa8 It can not only print JSON data in a pretty way,\n\n\ud83d\udd04 But also convert JSON, TOML, XML and YAML data formats to each other,\n\n\ud83d\udd0e And even extract content from JSON data using JMESPATH or JSONPATH.\n\n\ud83e\uddd0 You can even use **jsonfmt** to compare differences between two JSON or other formatted data.\n\n\n- [Quick Start](#quick-start)\n    - [Installation](#installation)\n    - [Usage](#usage)\n- [User Guide](#user-guide)\n    - [1. Pretty Print JSON Data](#1-pretty-print-json-data)\n    - [2. Minimize the JSON data](#2-minimize-the-json-data)\n    - [3. Extract Partial Content from JSON Data](#3-extract-partial-content-from-json-data)\n    - [4. Format Conversion](#4-format-conversion)\n    - [5. Diff Comparison](#5-diff-comparison)\n    - [6. Handle Large JSON Data Conveniently](#6-handle-large-json-data-conveniently)\n    - [7. Modify Values in Input Data](#7-modify-values-in-input-data)\n    - [8. Output to File](#8-output-to-file)\n- [TODO](#todo)\n\n\n## Quick Start\n\n### Installation\n\n```shell\n$ pip install jsonfmt\n```\n\n### Usage\n\n1. Process data from files.\n\n  ```shell\n  $ jf [options] [data_files ...]\n  ```\n\n2. Process data from `stdin`.\n\n  ```shell\n  $ echo '{\"hello\": \"world\"}' | jf [options]\n  ```\n\n**Positional Arguments**\n\n`files`: The data files to process, supporting JSON / TOML / XML / YAML formats.\n\n**Options**\n\n- `-h`: Show this help documentation and exit.\n- `-C`: CopyMode, which will copy the processing result to the clipboard.\n- `-d`: DiffMode, which compares the difference between the two input data.\n- `-D DIFFTOOL`: DifftoolMode, similar to \"DiffMode\". You can specify a tool to perform diff comparisons.\n- `-o`: OverviewMode, which can display an overview of the structure of the data, helping to quickly understand larger data.\n- `-O`: OverwriteMode, which will overwrite the original file with the formated text.\n- `-c`: Suppress all whitespace separation (most compact), only valid for JSON.\n- `-e`: Escape all characters to ASCII codes.\n- `-f`: The format to output (default: same as input data format, options: `json` / `toml` / `xml` / `yaml`).\n- `-i`: Number of spaces for indentation (default: 2, range: 0~8, set to 't' to use <kbd>Tab</kbd> as indentation).\n- `-l`: Query language for extracting data (default: auto-detect, options: jmespath / jsonpath).\n- `-p QUERYPATH`: JMESPath or JSONPath query path.\n- `-s`: Sort the output of dictionaries alphabetically by key.\n- `--set 'foo.k1=v1;k2[i]=v2'`: Key-value pairs to add or modify (separated by \";\").\n- `--pop 'k1;foo.k2;k3[i]'`: Key-value pairs to delete (separated by \";\").\n- `-v`: Show the version.\n\n\n## User Guide\n\nIn order to demonstrate the features of jsonfmt, we need to first create a test data and save it to the file example.json. The file contents are as follows:\n\n```json\n{\n    \"name\": \"Bob\",\n    \"age\": 23,\n    \"gender\": \"\u7eaf\u7237\u4eec\",\n    \"money\": 3.1415926,\n    \"actions\": [\n        {\n            \"name\": \"eating\",\n            \"calorie\": 1294.9,\n            \"date\": \"2021-03-02\"\n        },\n        {\n            \"name\": \"sporting\",\n            \"calorie\": -2375,\n            \"date\": \"2023-04-27\"\n        },\n        {\n            \"name\": \"sleeping\",\n            \"calorie\": -420.5,\n            \"date\": \"2023-05-15\"\n        }\n    ]\n}\n```\n\nThen, convert this data to TOML, XML and YAML formats, and save them as example.toml, example.xml and example.yaml respectively.\n\nThese data files can be found in the *test* folder of the source code:\n\n```\ntest/\n|- example.json\n|- example.toml\n|- example.xml\n|- example.yaml\n```\n\n### 1. Pretty Print JSON Data\n\n#### Syntax Highlighting and Indentation\n\nThe default working mode of jsonfmt is to format the data and print it with syntax highlighting.\n\nThe option `-i` specifies the number of spaces for indentation. By default, it is 2 spaces, and the number of spaces allowed is between 0 and 8. If you want to use the <kbd>tab</kbd> as indentation, set it to `t`.\n\nThe `-s` option is used to sort the dictionary alphabetically by key.\n\nIf there are some non-ASCII characters in the JSON data, you can use `-e` to escape them.\n\n```shell\n$ jf -s -i 4 test/example.json\n```\n\nOutput:\n\n```json\n{\n    \"actions\": [\n        {\n            \"calorie\": 1294.9,\n            \"date\": \"2021-03-02\",\n            \"name\": \"eating\"\n        },\n        {\n            \"calorie\": -2375,\n            \"date\": \"2023-04-27\",\n            \"name\": \"sporting\"\n        },\n        {\n            \"calorie\": -420.5,\n            \"date\": \"2023-05-15\",\n            \"name\": \"sleeping\"\n        }\n    ],\n    \"age\": 23,\n    \"gender\": \"\u7eaf\u7237\u4eec\",\n    \"money\": 3.1415926,\n    \"name\": \"Bob\"\n}\n```\n\n#### Read JSON from Pipeline\n\nSometimes the data to be processed comes from the output of other commands. Just use the pipe character `|` to connect the two commands and then take it from the `stdin`.\n\n```shell\n$ curl -s https://jsonplaceholder.typicode.com/posts/1 | jf -i 4\n```\n\nOutput:\n\n```json\n{\n    \"userId\": 1,\n    \"id\": 1,\n    \"title\": \"sunt aut facere repellat provident occaecati excepturi optio reprehenderit\",\n    \"body\": \"quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nrep...\"\n}\n\n```\n\n### 2. Minimize the JSON data\n\nThe `-c` option used to suppress all whitespace and newlines to compact the JSON data into a single line.\n\n```shell\n$ echo '{\n    \"name\": \"alex\",\n    \"age\": 21,\n    \"items\": [\n        \"pen\",\n        \"phone\"\n    ]\n}' | jf -c\n```\n\nOutput:\n\n```json\n{\"name\":\"alex\",\"age\":21,\"items\":[\"pen\",\"phone\"]}\n```\n\n### 3. Extract Partial Content from JSON Data\n\njsonfmt uses both [**JMESPath**](https://jmespath.org/) and [**JSONPath**](https://datatracker.ietf.org/doc/id/draft-goessner-dispatch-jsonpath-00.html) as its query languages.\n\n**JMESPath** (JSON Meta Language for Expression Path) is a query language introduced by AWS for processing JSON data. Among the many JSON query languages, JMESPath seems to be the most widely-used, fastest-growing, and highest-rated. Its syntax is more concise and more universal than [**jq**](https://jqlang.github.io/jq/), and its functionality is more powerful and feature-rich than JSONPath. Therefore, I prefer to use it as the primary JSON query language.\n\nJMESPath can elegantly use simple syntax to extract part of the content from JSON data, and also compose the filtered data into a new object or array. The official JMESPath tutorial is [here](https://jmespath.org/tutorial.html).\n\n#### JMESPath Examples\n\n- Extract the first item of `actions` from *example.json*:\n\n    ```shell\n    $ jf -p 'actions[0]' test/example.json\n    ```\n\n    Output:\n\n    ```json\n    {\n        \"name\": \"eating\",\n        \"calorie\": 1294.9,\n        \"date\": \"2021-03-02\"\n    }\n    ```\n\n- Filter all items with `calorie < 0` from `actions`.\n\n    ```shell\n    # Here, `0` means 0 is a number\n    $ jf -p 'actions[?calorie<`0`]' test/example.json\n    ```\n\n    Output:\n\n    ```json\n    [\n        {\n            \"name\": \"sporting\",\n            \"calorie\": -2375,\n            \"date\": \"2023-04-27\"\n        },\n        {\n            \"name\": \"sleeping\",\n            \"calorie\": -420.5,\n            \"date\": \"2023-05-15\"\n        }\n    ]\n    ```\n\n- Show all keys and the length of `actions`.\n\n    ```shell\n    $ jf -p '{all_keys:keys(@), actions_len:length(actions)}' test/example.json\n    ```\n\n    Output:\n\n    ```json\n    {\n        \"all_keys\": [\n            \"name\",\n            \"age\",\n            \"gender\",\n            \"money\",\n            \"actions\"\n        ],\n        \"actions_len\": 3\n    }\n    ```\n\n- Sort the items in `actions` by their `calorie` value, and define the result as a new dictionary.\n\n    ```shell\n    $ jf -p 'sort_by(actions, &calorie)[].{foo: name, bar:calorie}' test/example.json\n    ```\n\n    Output:\n\n    ```json\n    [\n        {\n            \"foo\": \"sporting\",\n            \"bar\": -2375\n        },\n        {\n            \"foo\": \"sleeping\",\n            \"bar\": -420.5\n        },\n        {\n            \"foo\": \"eating\",\n            \"bar\": 1294.9\n        }\n    ]\n    ```\n\n[More JMESPath examples](https://jmespath.org/examples.html).\n\n#### JSONPath Examples\n\nJSONPath was inspired by the design of XPath. Therefore, it can precisely locate any element in the JSON document through path expressions, similar to XPath, enabling efficient retrieval, filtering, and operation of complex nested data.\n\nUnlike the tag hierarchical structure of XML, JSONPath specially handles JSON key-value pairs and arrays, allowing users to conveniently access multi-level object properties, iterate over objects and arrays, and filter data based on conditions.\n\nSome queries that are difficult to handle with JMESPath can be easily achieved with JSONPath.\n\n- Filter all `name` fields using relative paths:\n\n    ```shell\n    # Use -l to specify the query language as JSONPath\n    $ jf -l jsonpath -p '$..name' test/example.json\n    ```\n\n    Output:\n\n    ```json\n    [\n        \"Bob\",\n        \"eating\",\n        \"sporting\",\n        \"sleeping\"\n    ]\n    ```\n\n#### Querying TOML, XML and YAML\n\nOne of the powerful features of jsonfmt is that you can process TOML, XML and YAML in exactly the same way as JSON, and freely convert the result format. You can even process these four formats simultaneously in one command.\n\n- Read data from a toml file and output in YAML format\n\n    ```shell\n    $ jf -p '{all_keys:keys(@), actions_len:length(actions)}' test/example.toml -f yaml\n    ```\n\n    Output:\n\n    ```yaml\n    all_keys:\n    - name\n    - age\n    - gender\n    - money\n    - actions\n    actions_len: 3\n    ```\n\n- Process three formats at once\n\n    ```shell\n    $ jf -p 'actions[0]' test/example.json test/example.toml test/example.yaml\n    ```\n\n    Output:\n\n    ```yaml\n    1. test/example.json\n    {\n        \"name\": \"eating\",\n        \"calorie\": 1294.9,\n        \"date\": \"2021-03-02\"\n    }\n\n    2. test/example.toml\n    name = \"eating\"\n    calorie = 1294.9\n    date = \"2021-03-02\"\n\n    3. test/example.xml\n    <?xml version=\"1.0\" ?>\n    <root>\n        <name>eating</name>\n        <calorie>1294.9</calorie>\n        <date>2021-03-02</date>\n    </root>\n\n    4. test/example.yaml\n    name: eating\n    calorie: 1294.9\n    date: '2021-03-02'\n    ```\n\n\n### 4. Format Conversion\n\n*jsonfmt* supports processing JSON, TOML, XML and YAML formats. Each format can be converted to other formats by specifying the \"-f\" option.\n\n<div style=\"color: orange\"><strong>Note:</strong></div>\n\n1. `null` is not supported in TOML. Therefore, all `null` values will be deleted when converting from other formats to TOML.\n\n2. XML does not support multi-dimensional arrays. Therefore, if the original data contains multi-dimensional arrays, a wrong data will be generated during the conversion to XML format.\n\n#### Example 1. JSON to YAML\n\n```shell\n$ jf test/example.json -f yaml\n```\n\nOutput:\n\n```yaml\nname: Bob\nage: 23\ngender: \u7eaf\u7237\u4eec\nmoney: 3.1415926\nactions:\n- name: eating\n  calorie: 1294.9\n  date: '2021-03-02'\n- name: sporting\n  calorie: -2375\n  date: '2023-04-27'\n- name: sleeping\n  calorie: -420.5\n  date: '2023-05-15'\n```\n\n#### Example 2. TOML to XML\n\n```shell\n$ jf test/example.toml -f xml\n```\n\nOutput:\n\n```xml\n<?xml version=\"1.0\" ?>\n<root>\n    <name>Bob</name>\n    <age>23</age>\n    <gender>\u7eaf\u7237\u4eec</gender>\n    <money>3.1415926</money>\n    <actions>\n        <name>eating</name>\n        <calorie>1294.9</calorie>\n        <date>2021-03-02</date>\n    </actions>\n    <actions>\n        <name>sporting</name>\n        <calorie>-2375</calorie>\n        <date>2023-04-27</date>\n    </actions>\n    <actions>\n        <name>sleeping</name>\n        <calorie>-420.5</calorie>\n        <date>2023-05-15</date>\n    </actions>\n</root>\n```\n\n\n### 5. Diff Comparison\n\nIn development, we often need to compare differences between some data or configurations. For example, compare the return results of an API when passing in different parameters, or compare the differences between system configuration files in different formats by operations personnel.\n\njsonfmt supports various diff-tools by default, such as `diff`, `vimdiff`, `git`, `code`, `kdiff3`, `meld`, and also supports `WinMerge` and `fc` on Windows, and other tools can also be supported through the `-D` option.\n\nBy default, jsonfmt will first check if git is installed on the computer. If git is available, jsonfmt will call `git config --global diff.tool` to read the configured diff-tool. If it's not set, it will use the default diff-tool of git for processing. If git is not available, it will search in the order of `code`, `kdiff3`, `meld`, `vimdiff`, `diff`, `WinMerge`, `fc`. If no available diff-tool is found, jsonfmt will exit with an error.\n\nIn DiffMode, jsonfmt will first format the data to be compared (at this time, the `-s` option will be automatically enabled), and save the result to a temporary file, and then call the specified tool for diff comparison.\n\n#### Example 1: Compare two JSON files\n\n```shell\n$ jf -d test/example.json test/another.json\n```\n\nOutput:\n\n```diff\n--- /tmp/.../jf-jjn86s7r_example.json     2024-03-23 18:22:00\n+++ /tmp/.../jf-vik3bqsu_another.json     2024-03-23 18:22:00\n@@ -3,21 +3,16 @@\n     {\n       \"calorie\": 1294.9,\n       \"date\": \"2021-03-02\",\n-      \"name\": \"eating\"\n+      \"name\": \"thinking\"\n     },\n     {\n-      \"calorie\": -2375,\n-      \"date\": \"2023-04-27\",\n-      \"name\": \"sporting\"\n-    },\n-    {\n       \"calorie\": -420.5,\n       \"date\": \"2023-05-15\",\n       \"name\": \"sleeping\"\n     }\n   ],\n   \"age\": 23,\n-  \"gender\": \"\u7eaf\u7237\u4eec\",\n+  \"gender\": \"male\",\n   \"money\": 3.1415926,\n-  \"name\": \"Bob\"\n+  \"name\": \"Tom\"\n }\n```\n\n#### Example 2: Specify diff-tool with `-D`\n\nThe `-D DIFFTOOL` option can specify a diff comparison tool. As long as its command format matches `command [options] file1 file2`, it doesn't matter whether it's in jsonfmt's default supported tool list or not.\n\n```shell\n$ jf -D sdiff test/example.json test/another.json\n```\n\nOutput:\n\n```\n{                                   {\n  \"actions\": [                        \"actions\": [\n    {                                   {\n      \"calorie\": 1294.9,                  \"calorie\": 1294.9,\n      \"date\": \"2021-03-02\",               \"date\": \"2021-03-02\",\n      \"name\": \"eating\"         |          \"name\": \"thinking\"\n    },                                  },\n    {                                   {\n      \"calorie\": -2375,        <\n      \"date\": \"2023-04-27\",    <\n      \"name\": \"sporting\"       <\n    },                         <\n    {                          <\n      \"calorie\": -420.5,                  \"calorie\": -420.5,\n      \"date\": \"2023-05-15\",               \"date\": \"2023-05-15\",\n      \"name\": \"sleeping\"                  \"name\": \"sleeping\"\n    }                                   }\n  ],                                  ],\n  \"age\": 23,                          \"age\": 23,\n  \"gender\": \"\u7eaf\u7237\u4eec\",          |      \"gender\": \"male\",\n  \"money\": 3.1415926,                 \"money\": 3.1415926,\n  \"name\": \"Bob\"                |      \"name\": \"Tom\"\n}                                   }\n```\n\n#### Example 3: Specify options for the selected tool\n\nIf you need to pass parameters to the diff-tool, you can use `-D 'DIFFTOOL OPTIONS'`.\n\n```shell\n$ jf -D 'diff --ignore-case --color=always' test/example.json test/another.json\n```\n\nOutput:\n\n```diff\n6c6\n<       \"name\": \"eating\"\n---\n>       \"name\": \"thinking\"\n9,13d8\n<       \"calorie\": -2375,\n<       \"date\": \"2023-04-27\",\n<       \"name\": \"sporting\"\n<     },\n<     {\n20c15\n<   \"gender\": \"\u7eaf\u7237\u4eec\",\n---\n>   \"gender\": \"male\",\n22c17\n<   \"name\": \"Bob\"\n---\n>   \"name\": \"Tom\"\n```\n\n#### Example 4: Compare data in different formats\n\nFor data from different sources, their formats, indentation, and key order may be different. In this case, you can use `-i` and `-f` together for diff comparison.\n\n```shell\n$ jf -d -i 4 -f toml test/example.toml test/another.json\n```\n\nOutput:\n\n```diff\n--- /var/.../jf-qw9vm33n_example.toml     2024-03-23 18:29:17\n+++ /var/.../jf-dqb_fl4x_another.json     2024-03-23 18:29:17\n@@ -1,18 +1,13 @@\n age = 23\n-gender = \"\u7eaf\u7237\u4eec\"\n+gender = \"male\"\n money = 3.1415926\n-name = \"Bob\"\n+name = \"Tom\"\n [[actions]]\n calorie = 1294.9\n date = \"2021-03-02\"\n-name = \"eating\"\n+name = \"thinking\"\n\n [[actions]]\n-calorie = -2375\n-date = \"2023-04-27\"\n-name = \"sporting\"\n-\n-[[actions]]\n calorie = -420.5\n date = \"2023-05-15\"\n name = \"sleeping\"\n```\n\n### 6. Handle Large JSON Data Conveniently\n\nVery often, JSON data from program interfaces is very large, which makes it difficult for us to read, debug, and process. jsonfmt provides four ways to handle large JSON data:\n\n- Use JMESPath or JSONPath to read part of the content ([already covered in the previous section](#3-extract-partial-content-from-json-data))\n- [Use pager mode to view larger JSON data](#use-pager-mode-to-view-larger-json-data)\n- [Show an overview of large JSON data](#show-an-overview-of-large-json-data)\n- [Copy the processing result to the clipboard](#copy-the-processing-result-to-the-clipboard)\n\n#### Use pager mode to view larger JSON data\n\nPager mode is similar to the `more` command. When the JSON data is too large to be fully displayed in the window area, *jsonfmt* will automatically display the result in pager mode.\n\nThe operations in pager mode are the same as the `more` command:\n\n| Key                                           | Description                    |\n|-----------------------------------------------|--------------------------------|\n| <kbd>j</kbd>                                  | Move forward one line          |\n| <kbd>k</kbd>                                  | Move backward one line         |\n| <kbd>f</kbd> or <kbd>ctrl</kbd>+<kbd>f</kbd>  | Move forward one page          |\n| <kbd>b</kbd>  or <kbd>ctrl</kbd>+<kbd>b</kbd> | Move backward one page         |\n| <kbd>g</kbd>                                  | Jump to the top of the page    |\n| <kbd>G</kbd>                                  | Jump to the bottom of the page |\n| <kbd>/</kbd>                                  | Search mode                    |\n| <kbd>q</kbd>                                  | Exit pager mode                |\n\nThe return value of this API below is a large JSON data, you can paste this command into the terminal to try the pager mode:\n\n```shell\n$ curl -s https://jsonplaceholder.typicode.com/users | jf\n```\n\n#### Show an overview of large JSON data\n\nSometimes we only want to see an overview of the JSON data without caring about the details. In this case, you can use the `-o` option. It will clear the sublists in the JSON, and replace the strings to `\"...\"` to show the overview.\n\nIf the root node of the JSON data is a list, only its first child element will be preserved in the overview.\n\n```shell\n$ jf -o test/example.json\n```\n\nOutput:\n\n```json\n{\n    \"name\": \"...\",\n    \"age\": 23,\n    \"gender\": \"...\",\n    \"money\": 3.1415926,\n    \"actions\": []\n}\n```\n\n#### Copy the processing result to the clipboard\n\nIf you want to paste the processed result into a file, but the output printed in the terminal exceeds one page, it may be difficult to copy. In this case, you can use the `-C` option to automatically copy the result to the clipboard.\n\n```shell\n$ jf -C test/example.json\n```\n\nAfter completing the above operation, you can use <kbd>ctrl</kbd>+<kbd>v</kbd> or <kbd>cmd</kbd>+<kbd>v</kbd> to paste the result into other documents.\n\n<div style=\"color: orange\"><strong>Note:</strong></div>\n\nWhen processing multiple targets at the same time, such as: `jf -C file1 file2 file3 ...`, jsonfmt will copy the processing results of all files to the clipboard, with two newline characters `'\\n\\n'` separating multiple results.\n\n### 7. Modify Values in Input Data\n\nWhen you need to change some content in the input document, use the `--set` and `--pop` options.\n\nThe format is `--set 'key=value'`. If you need to modify multiple values, you can separate them with `;`, like this: `--set 'k1=v1;k2=v2'`. If the key-value pair does not exist, it will be added.\n\nFor items in a list, use `key[i]` or `key.i` to specify. If the index is greater or equal to the number of elements, the value will be appended.\n\n#### Add key-value pairs\n\n```shell\n# Add country = China, and append an item to actions\n$ jf --set 'country=China; actions[3]={\"name\": \"drinking\"}' test/example.json\n```\n\nOutput:\n\n```json\n{\n    \"name\": \"Bob\",\n    \"age\": 23,\n    \"gender\": \"\u7eaf\u7237\u4eec\",\n    \"money\": 3.1415926,\n    \"actions\": [\n        {\n            \"name\": \"eating\",\n            \"calorie\": 1294.9,\n            \"date\": \"2021-03-02\"\n        },\n        {\n            \"name\": \"sporting\",\n            \"calorie\": -2375,\n            \"date\": \"2023-04-27\"\n        },\n        {\n            \"name\": \"sleeping\",\n            \"calorie\": -420.5,\n            \"date\": \"2023-05-15\"\n        },\n        {\n            \"name\": \"drinking\"\n        }\n    ],\n    \"country\": \"China\"\n}\n```\n\n#### Modify values\n\n```shell\n# Modify money and actions[1][\"name\"]\n$ jf --set 'money=1000; actions[1].name=swim' test/example.json\n```\n\nOutput:\n\n```json\n{\n    \"name\": \"Bob\",\n    \"age\": 23,\n    \"gender\": \"\u7eaf\u7237\u4eec\",\n    \"money\": 1000,\n    \"actions\": [\n        {\n            \"name\": \"eating\",\n            \"calorie\": 1294.9,\n            \"date\": \"2021-03-02\"\n        },\n        {\n            \"name\": \"swim\",\n            \"calorie\": -2375,\n            \"date\": \"2023-04-27\"\n        },\n        {\n            \"name\": \"sleeping\",\n            \"calorie\": -420.5,\n            \"date\": \"2023-05-15\"\n        }\n    ]\n}\n```\n\n#### Delete key-value pairs\n\n```shell\n# Delete gender and actions[1]\n$ jf --pop 'gender; actions[1]' test/example.json\n```\n\nOutput:\n\n```json\n{\n    \"name\": \"Bob\",\n    \"age\": 23,\n    \"money\": 3.1415926,\n    \"actions\": [\n        {\n            \"name\": \"eating\",\n            \"calorie\": 1294.9,\n            \"date\": \"2021-03-02\"\n        },\n        {\n            \"name\": \"sleeping\",\n            \"calorie\": -420.5,\n            \"date\": \"2023-05-15\"\n        }\n    ]\n}\n```\n\nOf course, you can also use `--set` and `--pop` at the same time:\n\n```shell\njf --set 'skills=[\"Django\",\"Flask\"];money=1000' --pop 'gender;actions[1]' test/example.json\n```\n\n<div style=\"color: orange\"><strong>Note:</strong></div>\nThe above command will not modify the original JSON file. If you want to do so, see below.\n\n### 8. Output to File\n\njsonfmt does not provide a dedicated option to write the processing result to a file. Because you can easily handle this by using the terminal's redirection symbol `>`, which is supported on both Linux and Windows.\n\n```shell\n$ jf -si 4 test/example.json > formatted.json\n```\n\nIf you need to overwrite the processed result to the original file, you can use the `-O` option:\n\n```shell\n# Sort by object keys, set indentation to 4 spaces, set the name value to Alex, and write the final result to the original file\n$ jf -s -i 4 --set 'name=Alex' -O test/example.json\n```\n\n## TODO\n\n- [ ] Add URL support to directly compare data from two APIs\n- [ ] Add INI format support\n- [ ] Add merge mode to combine multiple JSON or other formatted data into one\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "A powerful tool for pretty-printing, querying and conversion JSON documents.",
    "version": "0.2.7",
    "project_urls": {
        "documentation": "https://seamile.github.io/jsonfmt/",
        "homepage": "https://github.com/seamile/jsonfmt",
        "repository": "https://github.com/seamile/jsonfmt"
    },
    "split_keywords": [
        "json",
        " formatter",
        " pretty-print",
        " highlight",
        " jmespath"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "797dd5566747f0e7054f86653a6299af298a1e59b41e732844c06cf61cf83cbc",
                "md5": "216aef5423790a56c61f7e4f96edf5c1",
                "sha256": "5f3b8110b5ad1940da0be9ad6a0cd4313e9371e07c2d7416255e9e9e78989991"
            },
            "downloads": -1,
            "filename": "jsonfmt-0.2.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "216aef5423790a56c61f7e4f96edf5c1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 18144,
            "upload_time": "2024-04-10T07:18:50",
            "upload_time_iso_8601": "2024-04-10T07:18:50.941937Z",
            "url": "https://files.pythonhosted.org/packages/79/7d/d5566747f0e7054f86653a6299af298a1e59b41e732844c06cf61cf83cbc/jsonfmt-0.2.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bec2c31d1fad0757bd9747c51eb0686634a34745d3c93f812d5b87b2558074dc",
                "md5": "32d643144dcbf93f6d4fb19efca16997",
                "sha256": "da7340400ef016052e3fa2caeac7d07c521da17e325995f085253eea469b5b4d"
            },
            "downloads": -1,
            "filename": "jsonfmt-0.2.7.tar.gz",
            "has_sig": false,
            "md5_digest": "32d643144dcbf93f6d4fb19efca16997",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 31243,
            "upload_time": "2024-04-10T07:18:53",
            "upload_time_iso_8601": "2024-04-10T07:18:53.679629Z",
            "url": "https://files.pythonhosted.org/packages/be/c2/c31d1fad0757bd9747c51eb0686634a34745d3c93f812d5b87b2558074dc/jsonfmt-0.2.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-10 07:18:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "seamile",
    "github_project": "jsonfmt",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "jmespath",
            "specs": [
                [
                    ">=",
                    "0.10.0"
                ]
            ]
        },
        {
            "name": "jsonpath-ng",
            "specs": [
                [
                    ">=",
                    "1.5.3"
                ]
            ]
        },
        {
            "name": "Pygments",
            "specs": [
                [
                    ">=",
                    "2.13.0"
                ]
            ]
        },
        {
            "name": "pyperclip",
            "specs": [
                [
                    ">=",
                    "1.8.2"
                ]
            ]
        },
        {
            "name": "pyyaml",
            "specs": [
                [
                    ">=",
                    "6.0"
                ]
            ]
        },
        {
            "name": "toml",
            "specs": [
                [
                    ">=",
                    "0.10.2"
                ]
            ]
        }
    ],
    "lcname": "jsonfmt"
}
        
Elapsed time: 0.22688s