8 breaking releases

0.9.0 Nov 22, 2024
0.7.0 Dec 20, 2023
0.6.0 Sep 29, 2023
0.5.0 Apr 23, 2023
0.3.1 Nov 13, 2022

#326 in Text processing

Download history 901/week @ 2024-08-19 1029/week @ 2024-08-26 650/week @ 2024-09-02 589/week @ 2024-09-09 298/week @ 2024-09-16 349/week @ 2024-09-23 369/week @ 2024-09-30 714/week @ 2024-10-07 405/week @ 2024-10-14 673/week @ 2024-10-21 639/week @ 2024-10-28 695/week @ 2024-11-04 475/week @ 2024-11-11 558/week @ 2024-11-18 531/week @ 2024-11-25 429/week @ 2024-12-02

2,033 downloads per month
Used in 4 crates (2 directly)

MIT license

1MB
18K 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)
        .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  | | |
| |      |               |             | +-------+ | |
| +------+---------------+-------------+-----------+ |
+----------------------------------------------------+

Dependencies

~3MB
~54K SLoC