json_to_table

Crates.iojson_to_table
lib.rsjson_to_table
version0.9.0
sourcesrc
created_at2022-09-30 15:31:12.480885
updated_at2024-11-22 21:21:50.456591
descriptionA library for pretty print JSON as a table
homepagehttps://github.com/zhiburt/tabled
repositoryhttps://github.com/zhiburt/tabled
max_upload_size
id677381
size306,750
Maxim Zhiburt (zhiburt)

documentation

https://docs.rs/json_to_table

README

A library for converting json to a table.

It uses tabled as a rendering backend.

Usage

Add the library to a dependency list.

[dependencies]
json_to_table = "0.6"

The main and only function you shall use to build a table is json_to_table.

Example Result
use json_to_table::json_to_table;
use serde_json::json;

fn main() {
    let value = json!(
        [
            {
                "name": "Aleix Melon",
                "id": "E00245",
                "role": ["Dev", "DBA"],
                "age": 23,
                "doj": "11-12-2019",
                "married": false,
                "address": {
                    "street": "32, Laham St.",
                    "city": "Innsbruck",
                    "country": "Austria"
                    },
                "referred-by": "E0012"
            },
        ]
    );

    let table = json_to_table(&value).to_string();

    println!("{}", table)
}
+-------------------------------------------------+
| +-------------+-------------------------------+ |
| | address     | +---------+-----------------+ | |
| |             | | city    |  Innsbruck      | | |
| |             | +---------+-----------------+ | |
| |             | | country |  Austria        | | |
| |             | +---------+-----------------+ | |
| |             | | street  |  32, Laham St.  | | |
| |             | +---------+-----------------+ | |
| +-------------+-------------------------------+ |
| | age         |  23                           | |
| +-------------+-------------------------------+ |
| | doj         |  11-12-2019                   | |
| +-------------+-------------------------------+ |
| | id          |  E00245                       | |
| +-------------+-------------------------------+ |
| | married     |  false                        | |
| +-------------+-------------------------------+ |
| | name        |  Aleix Melon                  | |
| +-------------+-------------------------------+ |
| | referred-by |  E0012                        | |
| +-------------+-------------------------------+ |
| | role        | +-------+                     | |
| |             | |  Dev  |                     | |
| |             | +-------+                     | |
| |             | |  DBA  |                     | |
| |             | +-------+                     | |
| +-------------+-------------------------------+ |
+-------------------------------------------------+

You can also build a table in a squash mode.

Example Result
use json_to_table::json_to_table;
use serde_json::json;

fn main() {
    let value = json!(
        [
            {
                "name": "Aleix Melon",
                "id": "E00245",
                "role": ["Dev", "DBA"],
                "age": 23,
                "doj": "11-12-2019",
                "married": false,
                "address": {
                    "street": "32, Laham St.",
                    "city": "Innsbruck",
                    "country": "Austria"
                    },
                "referred-by": "E0012"
            },
        ]
    );

    let table = json_to_table(&value).collapse().to_string();

    println!("{}", table)
}
+-------------+---------+---------------+
| address     | city    | Innsbruck     |
|             +---------+---------------+
|             | country | Austria       |
|             +---------+---------------+
|             | street  | 32, Laham St. |
+-------------+---------+---------------+
| age         | 23                      |
+-------------+-------------------------+
| doj         | 11-12-2019              |
+-------------+-------------------------+
| id          | E00245                  |
+-------------+-------------------------+
| married     | false                   |
+-------------+-------------------------+
| name        | Aleix Melon             |
+-------------+-------------------------+
| referred-by | E0012                   |
+-------------+-------------------------+
| role        | Dev                     |
|             +-------------------------+
|             | DBA                     |
+-------------+-------------------------+

You can chose how to build an Array and Object via Orientation.

Example Result
use json_to_table::{json_to_table, Orientation};
use serde_json::json;

fn main() {
    let value = json!(
        [
            {
                "name": "Aleix Melon",
                "role": ["Dev", "DBA"],
                "age": 23,
                "referred-by": "E0012"
            },
            {
                "name": "Aleix Melon",
                "role": ["DBA"],
                "age": 24,
                "referred-by": "E0012"
            },
        ]
    );

    let table = json_to_table(&value)
        .object_orientation(Orientation::Row)
        .to_string();

    println!("{}", table)
}
+----------------------------------------------------+
| +------+---------------+-------------+-----------+ |
| | age  | name          | referred-by | role      | |
| +------+---------------+-------------+-----------+ |
| |  23  |  Aleix Melon  |  E0012      | +-------+ | |
| |      |               |             | |  Dev  | | |
| |      |               |             | +-------+ | |
| |      |               |             | |  DBA  | | |
| |      |               |             | +-------+ | |
| +------+---------------+-------------+-----------+ |
+----------------------------------------------------+
| +------+---------------+-------------+-----------+ |
| | age  | name          | referred-by | role      | |
| +------+---------------+-------------+-----------+ |
| |  24  |  Aleix Melon  |  E0012      | +-------+ | |
| |      |               |             | |  DBA  | | |
| |      |               |             | +-------+ | |
| +------+---------------+-------------+-----------+ |
+----------------------------------------------------+
Commit count: 1308

cargo fmt