8 releases (breaking)

0.7.0 Dec 20, 2023
0.6.0 Sep 29, 2023
0.5.0 Apr 23, 2023
0.4.0 Apr 11, 2023
0.1.0 Sep 30, 2022

#414 in Visualization

Download history 30/week @ 2023-12-22 38/week @ 2023-12-29 44/week @ 2024-01-05 32/week @ 2024-01-12 32/week @ 2024-01-19 29/week @ 2024-01-26 30/week @ 2024-02-02 39/week @ 2024-02-09 36/week @ 2024-02-16 57/week @ 2024-02-23 50/week @ 2024-03-01 80/week @ 2024-03-08 71/week @ 2024-03-15 44/week @ 2024-03-22 73/week @ 2024-03-29 33/week @ 2024-04-05

248 downloads per month

MIT license

1MB
17K SLoC

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)
        .set_object_mode(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  | | |
| |      |               |             | +-------+ | |
| +------+---------------+-------------+-----------+ |
+----------------------------------------------------+

Dependencies

~0.7–1.4MB
~30K SLoC