Repository: zhiburt/tabled
Pull request #343 (2023-06-22 13:38:18)

json_to_table/ Patch json to table add plain conversion into table
ref #342


Patch:
diff --git a/json_to_table/Cargo.toml b/json_to_table/Cargo.toml
index 5fca02f..5b50755 100644
--- a/json_to_table/Cargo.toml
+++ b/json_to_table/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "json_to_table"
-version = "0.5.0"
+version = "0.6.0"
 edition = "2018"
 authors = ["Maxim Zhiburt <zhiburt@gmail.com>"]
 repository = "https://github.com/zhiburt/tabled"
diff --git a/json_to_table/examples/json_plain.rs b/json_to_table/examples/json_plain.rs
new file mode 100644
index 0000000..f8d96f1
--- /dev/null
+++ b/json_to_table/examples/json_plain.rs
@@ -0,0 +1,24 @@
+//! This example demonstrates using the [`json_to_table::parse`] function
+//! to create a table as not recursive structure.
+
+use tabled::settings::Style;
+
+fn main() {
+    let json = serde_json::json!({
+        "key1": "value1",
+        "key2": {
+            "key1": 123,
+            "key2": [1, 2, 3, 4, 5],
+        },
+        "key3": [
+            {"key": 123.3},
+            2,
+            "asd"
+        ],
+    });
+
+    let mut table = json_to_table::parse(&json);
+    table.with(Style::modern());
+
+    println!("{table}");
+}
diff --git a/json_to_table/examples/json_width.rs b/json_to_table/examples/json_width.rs
new file mode 100644
index 0000000..ca907ef
--- /dev/null
+++ b/json_to_table/examples/json_width.rs
@@ -0,0 +1,25 @@
+//! This example demonstrates using the [`json_to_table::JsonTable::into_table`] function
+//! to create a table and control the width of certain fields.
+
+use tabled::settings::{Modify, Style, Width};
+
+fn main() {
+    let json = serde_json::json!({
+        "key1": "Some loooooooooooong string",
+        "key2": {
+            "key1": 123,
+            "key2": [1, 2, 3, 4, 5],
+        },
+        "key3": [
+            {"key": 123.3},
+            2,
+            "asd"
+        ],
+    });
+
+    let mut table = json_to_table::json_to_table(&json).into_table();
+    table.with(Style::extended());
+    table.with(Modify::new((0, 1)).with(Width::wrap(6)));
+
+    println!("{table}");
+}
diff --git a/json_to_table/src/lib.rs b/json_to_table/src/lib.rs
index e304fe7..6b311ff 100644
--- a/json_to_table/src/lib.rs
+++ b/json_to_table/src/lib.rs
@@ -90,14 +90,146 @@
 use serde_json::Value;
 
 pub use table::{JsonTable, Orientation};
+use tabled::{builder::Builder, Table};
 
 mod table;
 
-/// The function converts a given [`Value`] to a [`Table`].
+/// The function converts a given [`Value`] to a [`JsonTable`].
 ///
-/// See the example in a module documentation.
+/// ```
+/// let json = serde_json::json!({
+///     "key1": "value1",
+///     "key2": {
+///         "key1": 123,
+///         "key2": [1, 2, 3, 4, 5],
+///     },
+///     "key3": [
+///         {"key": 123.3},
+///         2,
+///         "asd"
+///     ],
+///     "key4": 1234.567
+/// });
+///     
+/// let table = json_to_table::json_to_table(&json).to_string();
 ///
-/// [`Table`]: tabled::Table
+/// assert_eq!(
+///     table,
+///     concat!(
+///         "+------+-----------------------+\n",
+///         "| key1 |  value1               |\n",
+///         "+------+-----------------------+\n",
+///         "| key2 | +------+---------+    |\n",
+///         "|      | | key1 |  123    |    |\n",
+///         "|      | +------+---------+    |\n",
+///         "|      | | key2 | +-----+ |    |\n",
+///         "|      | |      | |  1  | |    |\n",
+///         "|      | |      | +-----+ |    |\n",
+///         "|      | |      | |  2  | |    |\n",
+///         "|      | |      | +-----+ |    |\n",
+///         "|      | |      | |  3  | |    |\n",
+///         "|      | |      | +-----+ |    |\n",
+///         "|      | |      | |  4  | |    |\n",
+///         "|      | |      | +-----+ |    |\n",
+///         "|      | |      | |  5  | |    |\n",
+///         "|      | |      | +-----+ |    |\n",
+///         "|      | +------+---------+    |\n",
+///         "+------+-----------------------+\n",
+///         "| key3 | +-------------------+ |\n",
+///         "|      | | +-----+---------+ | |\n",
+///         "|      | | | key |  123.3  | | |\n",
+///         "|      | | +-----+---------+ | |\n",
+///         "|      | +-------------------+ |\n",
+///         "|      | |  2                | |\n",
+///         "|      | +-------------------+ |\n",
+///         "|      | |  asd              | |\n",
+///         "|      | +-------------------+ |\n",
+///         "+------+-----------------------+\n",
+///         "| key4 |  1234.567             |\n",
+///         "+------+-----------------------+",
+///     ),
+/// )
+/// ```
 pub fn json_to_table(value: &Value) -> JsonTable<&Value> {
     JsonTable::new(value)
 }
+
+/// The function converts a given [`Value`] to a [`Table`].
+///
+/// It's quite different from [`json_to_table`], cause it is not recursive
+/// and treats `json` `object` and `array` as string values.
+///
+/// ```
+/// let json = serde_json::json!({
+///     "key1": "value1",
+///     "key2": {
+///         "key1": 123,
+///         "key2": [1, 2, 3, 4, 5],
+///     },
+///     "key3": [
+///         {"key": 123.3},
+///         2,
+///         "asd"
+///     ],
+///     "key4": 1234.567
+/// });
+///     
+/// let table = json_to_table::parse(&json).to_string();
+///
+/// assert_eq!(
+///     table,
+///     concat!(
+///         "+------+---------------------------------+\n",
+///         "| key1 | value1                          |\n",
+///         "+------+---------------------------------+\n",
+///         "| key2 | {\"key1\":123,\"key2\":[1,2,3,4,5]} |\n",
+///         "+------+---------------------------------+\n",
+///         "| key3 | [{\"key\":123.3},2,\"asd\"]         |\n",
+///         "+------+---------------------------------+\n",
+///         "| key4 | 1234.567                        |\n",
+///         "+------+---------------------------------+",
+///     ),
+/// )
+/// ```
+///
+/// [`Table`]: tabled::Table
+pub fn parse(value: &Value) -> Table {
+    json_into_table(value)
+}
+
+fn json_into_table(value: &Value) -> Table {
+    match value {
+        Value::Array(array) => {
+            let list = array.iter().map(json_value_to_string).collect::<Vec<_>>();
+            Builder::from(vec![list]).build()
+        }
+        Value::Object(map) => {
+            let list = map
+                .iter()
+                .map(|(key, value)| vec![key.clone(), json_value_to_string(value)])
+                .collect::<Vec<_>>();
+
+            Builder::from(list).build()
+        }
+        Value::Null => Builder::default().build(),
+        Value::Bool(value) => single_value_table(value),
+        Value::Number(value) => single_value_table(value),
+        Value::String(value) => single_value_table(value),
+    }
+}
+
+fn single_value_table<V: ToString>(value: V) -> Table {
+    Builder::from(vec![vec![value.to_string()]]).build()
+}
+
+fn json_value_to_string(value: &Value) -> String {
+    match value {
+        Value::Null => String::new(),
+        Value::Bool(value) => value.to_string(),
+        Value::Number(value) => value.to_string(),
+        Value::String(value) => value.to_string(),
+        Value::Array(_) | Value::Object(_) => {
+            serde_json::to_string(value).unwrap_or_else(|_| format!("{:?}", value))
+        }
+    }
+}
diff --git a/json_to_table/src/table/mod.rs b/json_to_table/src/table/mod.rs
index 081ce21..1e2b85b 100644
--- a/json_to_table/src/table/mod.rs
+++ b/json_to_table/src/table/mod.rs
@@ -3,12 +3,14 @@ use std::borrow::Borrow;
 
 use serde_json::Value;
 use tabled::{
+    builder::Builder,
     grid::{
         config::{AlignmentHorizontal, ColoredConfig, Entity, Indent, Sides, SpannedConfig},
         dimension::CompleteDimension,
         records::EmptyRecords,
     },
     settings::{style::Style, TableOption},
+    tables::{PoolTable, TableValue},
     Table,
 };
 
@@ -25,6 +27,42 @@ pub struct JsonTable<T> {
 
 impl<T> JsonTable<T> {
     /// Creates a new [`JsonTable`] object.
+    ///
+    /// ```
+    /// use serde_json::json;
+    /// use json_to_table::JsonTable;
+    ///
+    /// let value = json!([
+    ///     123,
+    ///     ["123", "234", "456"],
+    ///     {"k1": 1, "k2": 2},
+    /// ]);
+    ///
+    /// let table = JsonTable::new(&value).to_string();
+    ///
+    /// assert_eq!(
+    ///     table,
+    ///     concat!(
+    ///         "+--------------+\n",
+    ///         "|  123         |\n",
+    ///         "+--------------+\n",
+    ///         "| +-------+    |\n",
+    ///         "| |  123  |    |\n",
+    ///         "| +-------+    |\n",
+    ///         "| |  234  |    |\n",
+    ///         "| +-------+    |\n",
+    ///         "| |  456  |    |\n",
+    ///         "| +-------+    |\n",
+    ///         "+--------------+\n",
+    ///         "| +----+-----+ |\n",
+    ///         "| | k1 |  1  | |\n",
+    ///         "| +----+-----+ |\n",
+    ///         "| | k2 |  2  | |\n",
+    ///         "| +----+-----+ |\n",
+    ///         "+--------------+",
+    ///     ),
+    /// );
+    /// ```
     pub fn new(value: T) -> Self {
         JsonTable {
             value,
@@ -38,18 +76,181 @@ impl<T> JsonTable<T> {
     }
 
     /// Collapse tables out instead of tables within tables.
+    ///
+    /// ```
+    /// use serde_json::json;
+    /// use json_to_table::json_to_table;
+    ///
+    /// let value = json!({
+    ///     "key1": 123,
+    ///     "234": ["123", "234", "456"],
+    ///     "key22": {
+    ///         "k1": 1,
+    ///         "k2": 2,
+    ///     }
+    /// });
+    ///
+    /// let table = json_to_table(&value)
+    ///     .collapse()
+    ///     .to_string();
+    ///
+    /// assert_eq!(
+    ///     table,
+    ///     concat!(
+    ///         "+-------+--------+\n",
+    ///         "| 234   | 123    |\n",
+    ///         "|       +--------+\n",
+    ///         "|       | 234    |\n",
+    ///         "|       +--------+\n",
+    ///         "|       | 456    |\n",
+    ///         "+-------+--------+\n",
+    ///         "| key1  | 123    |\n",
+    ///         "+-------+----+---+\n",
+    ///         "| key22 | k1 | 1 |\n",
+    ///         "|       +----+---+\n",
+    ///         "|       | k2 | 2 |\n",
+    ///         "+-------+----+---+",
+    ///     ),
+    /// );
+    /// ```
     pub fn collapse(&mut self) -> &mut Self {
         self.cfg.plain = false;
         self
     }
 
     /// Set a table mode for a [`serde_json::Value::Object`].
+    ///
+    /// ```
+    /// use serde_json::json;
+    /// use json_to_table::{json_to_table, Orientation};
+    ///
+    /// let value = json!({
+    ///     "key1": 123,
+    ///     "234": ["123", "234", "456"],
+    ///     "key22": {
+    ///         "k1": 1,
+    ///         "k2": 2,
+    ///     }
+    /// });
+    ///
+    /// let table = json_to_table(&value)
+    ///     .object_orientation(Orientation::Row)
+    ///     .to_string();
+    ///
+    /// assert_eq!(
+    ///     table,
+    ///     concat!(
+    ///         "+-----------+-------+---------------+\n",
+    ///         "| 234       | key1  | key22         |\n",
+    ///         "+-----------+-------+---------------+\n",
+    ///         "| +-------+ |  123  | +-----+-----+ |\n",
+    ///         "| |  123  | |       | | k1  | k2  | |\n",
+    ///         "| +-------+ |       | +-----+-----+ |\n",
+    ///         "| |  234  | |       | |  1  |  2  | |\n",
+    ///         "| +-------+ |       | +-----+-----+ |\n",
+    ///         "| |  456  | |       |               |\n",
+    ///         "| +-------+ |       |               |\n",
+    ///         "+-----------+-------+---------------+",
+    ///     ),
+    /// );
+    ///
+    /// let table = json_to_table(&value)
+    ///     .object_orientation(Orientation::Column)
+    ///     .to_string();
+    ///
+    /// assert_eq!(
+    ///     table,
+    ///     concat!(
+    ///         "+-------+--------------+\n",
+    ///         "| 234   | +-------+    |\n",
+    ///         "|       | |  123  |    |\n",
+    ///         "|       | +-------+    |\n",
+    ///         "|       | |  234  |    |\n",
+    ///         "|       | +-------+    |\n",
+    ///         "|       | |  456  |    |\n",
+    ///         "|       | +-------+    |\n",
+    ///         "+-------+--------------+\n",
+    ///         "| key1  |  123         |\n",
+    ///         "+-------+--------------+\n",
+    ///         "| key22 | +----+-----+ |\n",
+    ///         "|       | | k1 |  1  | |\n",
+    ///         "|       | +----+-----+ |\n",
+    ///         "|       | | k2 |  2  | |\n",
+    ///         "|       | +----+-----+ |\n",
+    ///         "+-------+--------------+",
+    ///     ),
+    /// );
+    /// ```
     pub fn object_orientation(&mut self, mode: Orientation) -> &mut Self {
         self.cfg.object_orientation = mode;
         self
     }
 
     /// Set a table mode for a [`serde_json::Value::Array`].
+    ///
+    /// ```
+    /// use serde_json::json;
+    /// use json_to_table::{json_to_table, Orientation};
+    ///
+    /// let value = json!({
+    ///     "key1": 123,
+    ///     "234": ["123", "234", "456"],
+    ///     "key22": {
+    ///         "k1": 1,
+    ///         "k2": 2,
+    ///     }
+    /// });
+    ///
+    /// let table = json_to_table(&value)
+    ///     .array_orientation(Orientation::Row)
+    ///     .to_string();
+    ///
+    /// assert_eq!(
+    ///     table,
+    ///     concat!(
+    ///         "+-------+---------------------------+\n",
+    ///         "| 234   | +-------+-------+-------+ |\n",
+    ///         "|       | |  123  |  234  |  456  | |\n",
+    ///         "|       | +-------+-------+-------+ |\n",
+    ///         "+-------+---------------------------+\n",
+    ///         "| key1  |  123                      |\n",
+    ///         "+-------+---------------------------+\n",
+    ///         "| key22 | +----+-----+              |\n",
+    ///         "|       | | k1 |  1  |              |\n",
+    ///         "|       | +----+-----+              |\n",
+    ///         "|       | | k2 |  2  |              |\n",
+    ///         "|       | +----+-----+              |\n",
+    ///         "+-------+---------------------------+",
+    ///     ),
+    /// );
+    ///
+    /// let table = json_to_table(&value)
+    ///     .array_orientation(Orientation::Column)
+    ///     .to_string();
+    ///
+    /// assert_eq!(
+    ///     table,
+    ///     concat!(
+    ///         "+-------+--------------+\n",
+    ///         "| 234   | +-------+    |\n",
+    ///         "|       | |  123  |    |\n",
+    ///         "|       | +-------+    |\n",
+    ///         "|       | |  234  |    |\n",
+    ///         "|       | +-------+    |\n",
+    ///         "|       | |  456  |    |\n",
+    ///         "|       | +-------+    |\n",
+    ///         "+-------+--------------+\n",
+    ///         "| key1  |  123         |\n",
+    ///         "+-------+--------------+\n",
+    ///         "| key22 | +----+-----+ |\n",
+    ///         "|       | | k1 |  1  | |\n",
+    ///         "|       | +----+-----+ |\n",
+    ///         "|       | | k2 |  2  | |\n",
+    ///         "|       | +----+-----+ |\n",
+    ///         "+-------+--------------+",
+    ///     ),
+    /// );
+    /// ```
     pub fn array_orientation(&mut self, mode: Orientation) -> &mut Self {
         self.cfg.array_orientation = mode;
         self
@@ -85,26 +286,24 @@ impl<T> JsonTable<T> {
     ///     .collapse()
     ///     .to_string();
     ///
-    /// println!("{}", table);
-    ///
-    ///    assert_eq!(
-    ///        table,
-    ///        concat!(
-    ///             "╔═════╦════╗\n",  
-    ///             "║  234║ 123║\n",  
-    ///             "║     ╠════╣\n",  
-    ///             "║     ║ 234║\n",  
-    ///             "║     ╠════╣\n",  
-    ///             "║     ║ 456║\n",  
-    ///             "╠═════╬════╣\n",  
-    ///             "║ key1║ 123║\n",  
-    ///             "╠═════╬══╦═╣\n",  
-    ///             "║key22║k1║1║\n",  
-    ///             "║     ╠══╬═╣\n",  
-    ///             "║     ║k2║2║\n",  
-    ///             "╚═════╩══╩═╝",
-    ///        ),
-    ///    );
+    /// assert_eq!(
+    ///     table,
+    ///     concat!(
+    ///          "╔═════╦════╗\n",  
+    ///          "║  234║ 123║\n",  
+    ///          "║     ╠════╣\n",  
+    ///          "║     ║ 234║\n",  
+    ///          "║     ╠════╣\n",  
+    ///          "║     ║ 456║\n",  
+    ///          "╠═════╬════╣\n",  
+    ///          "║ key1║ 123║\n",  
+    ///          "╠═════╬══╦═╣\n",  
+    ///          "║key22║k1║1║\n",  
+    ///          "║     ╠══╬═╣\n",  
+    ///          "║     ║k2║2║\n",  
+    ///          "╚═════╩══╩═╝",
+    ///     ),
+    /// );
     /// ```
     ///
     /// [`Table`]: tabled::Table
@@ -118,6 +317,175 @@ impl<T> JsonTable<T> {
 
         self
     }
+
+    /// Convert the table into a [`Table`].
+    ///
+    /// It does not recognizes collapsed mode.
+    ///
+    /// ```
+    /// use tabled::settings::style::Style;
+    /// use json_to_table::Orientation;
+    ///
+    /// let json = serde_json::json!({
+    ///     "key1": "value1",
+    ///     "key2": {
+    ///         "key1": 123,
+    ///         "key2": [1, 2, 3, 4, 5],
+    ///     },
+    ///     "key3": [
+    ///         {"key": 123.3},
+    ///         2,
+    ///         "asd"
+    ///     ],
+    ///     "key4": 1234.567
+    /// });
+    ///
+    /// let table = json_to_table::json_to_table(&json)
+    ///     .with(Style::modern())
+    ///     .array_orientation(Orientation::Row)
+    ///     .into_table()
+    ///     .with(Style::markdown())
+    ///     .to_string();
+    ///
+    /// assert_eq!(
+    ///     table,
+    ///     concat!(
+    ///         "| key1 | value1                                     |\n",
+    ///         "|------|--------------------------------------------|\n",
+    ///         "| key2 | ┌──────┬─────────────────────────────────┐ |\n",
+    ///         "|      | │ key1 │  123                            │ |\n",
+    ///         "|      | ├──────┼─────────────────────────────────┤ |\n",
+    ///         "|      | │ key2 │ ┌─────┬─────┬─────┬─────┬─────┐ │ |\n",
+    ///         "|      | │      │ │  1  │  2  │  3  │  4  │  5  │ │ |\n",
+    ///         "|      | │      │ └─────┴─────┴─────┴─────┴─────┘ │ |\n",
+    ///         "|      | └──────┴─────────────────────────────────┘ |\n",
+    ///         "| key3 | ┌───────────────────┬─────┬───────┐        |\n",
+    ///         "|      | │ ┌─────┬─────────┐ │  2  │  asd  │        |\n",
+    ///         "|      | │ │ key │  123.3  │ │     │       │        |\n",
+    ///         "|      | │ └─────┴─────────┘ │     │       │        |\n",
+    ///         "|      | └───────────────────┴─────┴───────┘        |\n",
+    ///         "| key4 | 1234.567                                   |",
+    ///     ),
+    /// )
+    /// ```
+    ///
+    /// Though it's said it's not suppose to work with collapsed tables;
+    /// It does;
+    /// Though it's a bit different.
+    ///
+    /// ```
+    /// use tabled::settings::style::Style;
+    /// use json_to_table::Orientation;
+    ///
+    /// let json = serde_json::json!({
+    ///     "key1": "value1",
+    ///     "key2": {
+    ///         "key1": 123,
+    ///         "key2": [1, 2, 3, 4, 5],
+    ///     },
+    ///     "key3": [
+    ///         {"key": 123.3},
+    ///         2,
+    ///         "asd"
+    ///     ],
+    ///     "key4": 1234.567
+    /// });
+    ///
+    /// let table = json_to_table::json_to_table(&json)
+    ///     .with(Style::modern())
+    ///     .array_orientation(Orientation::Row)
+    ///     .into_table()
+    ///     .with(Style::ascii_rounded())
+    ///     .to_string();
+    ///
+    /// assert_eq!(
+    ///     table,
+    ///     concat!(
+    ///         ".---------------------------------------------------.\n",
+    ///         "| key1 | value1                                     |\n",
+    ///         "| key2 | ┌──────┬─────────────────────────────────┐ |\n",
+    ///         "|      | │ key1 │  123                            │ |\n",
+    ///         "|      | ├──────┼─────────────────────────────────┤ |\n",
+    ///         "|      | │ key2 │ ┌─────┬─────┬─────┬─────┬─────┐ │ |\n",
+    ///         "|      | │      │ │  1  │  2  │  3  │  4  │  5  │ │ |\n",
+    ///         "|      | │      │ └─────┴─────┴─────┴─────┴─────┘ │ |\n",
+    ///         "|      | └──────┴─────────────────────────────────┘ |\n",
+    ///         "| key3 | ┌───────────────────┬─────┬───────┐        |\n",
+    ///         "|      | │ ┌─────┬─────────┐ │  2  │  asd  │        |\n",
+    ///         "|      | │ │ key │  123.3  │ │     │       │        |\n",
+    ///         "|      | │ └─────┴─────────┘ │     │       │        |\n",
+    ///         "|      | └───────────────────┴─────┴───────┘        |\n",
+    ///         "| key4 | 1234.567                                   |\n",
+    ///         "'---------------------------------------------------'",
+    ///     ),
+    /// )
+    /// ```
+    pub fn into_table(&self) -> Table
+    where
+        T: Borrow<Value>,
+    {
+        json_to_table(self.value.borrow(), &self.cfg)
+    }
+
+    /// Convert the table into a [`PoolTable`].
+    ///
+    /// It recognizes only collapsed mode.
+    ///
+    ///
+    /// ```
+    /// let json = serde_json::json!({
+    ///     "key1": "value1",
+    ///     "key2": {
+    ///         "key1": 123,
+    ///         "key2": [1, 2, 3, 4, 5],
+    ///     },
+    ///     "key3": [
+    ///         {"key": 123.3},
+    ///         2,
+    ///         "asd"
+    ///     ],
+    ///     "key4": 1234.567
+    /// });
+    ///
+    /// let table = json_to_table::json_to_table(&json)
+    ///     .into_pool_table()
+    ///     .to_string();
+    ///
+    /// assert_eq!(
+    ///     table,
+    ///     concat!(
+    ///        "+--------+-----------+\n",
+    ///        "| key1   | \"value1\"  |\n",
+    ///        "+-------++-----+-----+\n",
+    ///        "| key2  | key1 | 123 |\n",
+    ///        "|       +------++----+\n",
+    ///        "|       | key2  | 1  |\n",
+    ///        "|       |       +----+\n",
+    ///        "|       |       | 2  |\n",
+    ///        "|       |       +----+\n",
+    ///        "|       |       | 3  |\n",
+    ///        "|       |       +----+\n",
+    ///        "|       |       | 4  |\n",
+    ///        "|       |       +----+\n",
+    ///        "|       |       | 5  |\n",
+    ///        "+------++----+--+----+\n",
+    ///        "| key3 | key | 123.3 |\n",
+    ///        "|      +-----+-------+\n",
+    ///        "|      | 2           |\n",
+    ///        "|      +-------------+\n",
+    ///        "|      | \"asd\"       |\n",
+    ///        "+------+-+-----------+\n",
+    ///        "| key4   | 1234.567  |\n",
+    ///        "+--------+-----------+",
+    ///     ),
+    /// )
+    /// ```
+    pub fn into_pool_table(&self) -> PoolTable
+    where
+        T: Borrow<Value>,
+    {
+        json_to_pool_table(self.value.borrow(), &self.cfg)
+    }
 }
 
 impl<T> Display for JsonTable<T>
@@ -125,11 +493,29 @@ where
     T: Borrow<Value>,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        let table = json_to_table(self.value.borrow(), &self.cfg);
+        let table = json_to_string(self.value.borrow(), &self.cfg);
         table.fmt(f)
     }
 }
 
+impl<T> From<JsonTable<T>> for Table
+where
+    T: Borrow<Value>,
+{
+    fn from(val: JsonTable<T>) -> Self {
+        val.into_table()
+    }
+}
+
+impl<T> From<JsonTable<T>> for PoolTable
+where
+    T: Borrow<Value>,
+{
+    fn from(val: JsonTable<T>) -> Self {
+        val.into_pool_table()
+    }
+}
+
 #[derive(Debug, Clone)]
 struct Config {
     plain: bool,
@@ -165,9 +551,131 @@ fn configure_grid() -> SpannedConfig {
     cfg
 }
 
-fn json_to_table(value: &Value, cfg: &Config) -> String {
+fn json_to_string(value: &Value, cfg: &Config) -> String {
     match cfg.plain {
         true => plain_table::plain_table(value, cfg),
         false => collapsed_table::collapsed_table(value, cfg),
     }
 }
+
+fn json_to_table(value: &Value, cfg: &Config) -> Table {
+    match value {
+        Value::Array(array) => {
+            let list = array
+                .iter()
+                .map(|value| json_to_string_inner(value, cfg))
+                .collect::<Vec<_>>();
+
+            match cfg.array_orientation {
+                Orientation::Row => Builder::from(vec![list]).build(),
+                Orientation::Column => {
+                    let list = list
+                        .into_iter()
+                        .map(|value| vec![value])
+                        .collect::<Vec<_>>();
+                    Builder::from(list).build()
+                }
+            }
+        }
+        Value::Object(map) => {
+            let list = map
+                .iter()
+                .map(|(key, value)| vec![key.clone(), json_to_string_inner(value, cfg)])
+                .collect::<Vec<_>>();
+
+            match cfg.object_orientation {
+                Orientation::Row => {
+                    let (keys, values) = list.into_iter().fold(
+                        (Vec::with_capacity(map.len()), Vec::with_capacity(map.len())),
+                        |(mut keys, mut values), mut row| {
+                            let value = row.pop().unwrap();
+                            let key = row.pop().unwrap();
+                            keys.push(key);
+                            values.push(value);
+
+                            (keys, values)
+                        },
+                    );
+                    let list = vec![keys, values];
+
+                    Builder::from(list).build()
+                }
+                Orientation::Column => Builder::from(list).build(),
+            }
+        }
+        Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {
+            let value = json_value_to_string(value);
+            Builder::from(vec![vec![value]]).build()
+        }
+    }
+}
+
+fn json_to_pool_table(value: &Value, cfg: &Config) -> PoolTable {
+    let value = json_to_pool_table_value(value, cfg);
+    PoolTable::from(value)
+}
+
+fn json_to_pool_table_value(value: &Value, cfg: &Config) -> TableValue {
+    match value {
+        Value::Array(list) => {
+            let values = list
+                .iter()
+                .map(|value| json_to_pool_table_value(value, cfg))
+                .collect();
+
+            match cfg.array_orientation {
+                Orientation::Row => TableValue::Row(values),
+                Orientation::Column => TableValue::Column(values),
+            }
+        }
+        Value::Object(map) => match cfg.array_orientation {
+            Orientation::Row => {
+                let values = map
+                    .iter()
+                    .map(|(key, value)| {
+                        let key = TableValue::Cell(key.clone());
+                        let value = json_to_pool_table_value(value, cfg);
+                        TableValue::Column(vec![key, value])
+                    })
+                    .collect();
+
+                TableValue::Row(values)
+            }
+            Orientation::Column => {
+                let values = map
+                    .iter()
+                    .map(|(key, value)| {
+                        let key = TableValue::Cell(key.clone());
+                        let value = json_to_pool_table_value(value, cfg);
+                        TableValue::Row(vec![key, value])
+                    })
+                    .collect();
+
+                TableValue::Column(values)
+            }
+        },
+        value => TableValue::Cell(value.to_string()),
+    }
+}
+
+fn json_value_to_string(value: &Value) -> String {
+    match value {
+        Value::Null => String::new(),
+        Value::Bool(value) => value.to_string(),
+        Value::Number(value) => value.to_string(),
+        Value::String(value) => value.to_string(),
+        Value::Array(_) | Value::Object(_) => unreachable!(),
+    }
+}
+
+fn json_to_string_inner(value: &Value, cfg: &Config) -> String {
+    match cfg.plain {
+        true => match value {
+            Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {
+                json_value_to_string(value)
+            }
+            Value::Array(_) | Value::Object(_) => json_to_string(value, cfg),
+        },
+        false => json_to_string(value, cfg),
+    }
+}


Test patch:
diff --git a/json_to_table/tests/test_parse.rs b/json_to_table/tests/test_parse.rs
new file mode 100644
index 0000000..584583c
--- /dev/null
+++ b/json_to_table/tests/test_parse.rs
@@ -0,0 +1,176 @@
+use json_to_table::parse;
+use serde_json::json;
+use tabled::settings::Style;
+
+#[test]
+fn parse_json_1_test() {
+    let value = json!(
+        {"widget": {
+            "debug": "on",
+            "window": {
+                "height": 500
+            },
+            "image": {
+                "src": "Images/Sun.png",
+                "alignment": "center"
+            },
+            "text": {
+                "data": "Click Here",
+            }
+        }}
+    );
+
+    let table = parse(&value).with(Style::extended()).to_string();
+
+    assert_eq!(
+        table,
+        concat!(
+            "╔════════╦═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗\n",
+            "║ widget ║ {\"debug\":\"on\",\"image\":{\"alignment\":\"center\",\"src\":\"Images/Sun.png\"},\"text\":{\"data\":\"Click Here\"},\"window\":{\"height\":500}} ║\n",
+            "╚════════╩═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝",
+        )
+    );
+}
+
+#[test]
+fn parse_json_2_test() {
+    let value = json!(
+        {
+            "glossary": {
+                "title": "e",
+                "G": {
+                    "title": "S",
+                    "GlossList": {
+                        "GlossEntry": {
+                            "ID": "SGML",
+                            "GlossDef": {
+                                "GlossSeeAlso": ["GML", "XML"]
+                            },
+                            "GlossSee": "markup"
+                        }
+                    }
+                }
+            }
+        }
+    );
+
+    let table = parse(&value).with(Style::modern()).to_string();
+
+    assert_eq!(
+        table,
+        concat!(
+            "┌──────────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐\n",
+            "│ glossary │ {\"G\":{\"GlossList\":{\"GlossEntry\":{\"GlossDef\":{\"GlossSeeAlso\":[\"GML\",\"XML\"]},\"GlossSee\":\"markup\",\"ID\":\"SGML\"}},\"title\":\"S\"},\"title\":\"e\"} │\n",
+            "└──────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘",
+        )
+    );
+}
+
+#[test]
+fn general_json_3_test() {
+    let value = json!(
+        {
+            "header": "SVG Viewer",
+            "items": [
+                {"id": "Open"},
+                {"id": "OpenNew", "label": "Open New"},
+                null,
+                {"id": "Help"},
+                {"id": "About", "label": "About Adobe CVG Viewer..."}
+            ]
+        }
+    );
+
+    let table = parse(&value).with(Style::modern()).to_string();
+
+    assert_eq!(
+        table,
+        concat!(
+            "┌────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐\n",
+            "│ header │ SVG Viewer                                                                                                                │\n",
+            "├────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤\n",
+            "│ items  │ [{\"id\":\"Open\"},{\"id\":\"OpenNew\",\"label\":\"Open New\"},null,{\"id\":\"Help\"},{\"id\":\"About\",\"label\":\"About Adobe CVG Viewer...\"}] │\n",
+            "└────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘",
+        )
+    );
+}
+
+#[test]
+fn parse_map_empty_entity_plain_0() {
+    let value = json!(
+        {
+            "field1" : {},
+            "field2": "Value",
+            "field3" : {},
+            "field4": 3,
+            "field5" : {}
+        }
+    );
+
+    let table = parse(&value).with(Style::modern()).to_string();
+
+    assert_eq!(
+        table,
+        concat!(
+            "┌────────┬───────┐\n",
+            "│ field1 │ {}    │\n",
+            "├────────┼───────┤\n",
+            "│ field2 │ Value │\n",
+            "├────────┼───────┤\n",
+            "│ field3 │ {}    │\n",
+            "├────────┼───────┤\n",
+            "│ field4 │ 3     │\n",
+            "├────────┼───────┤\n",
+            "│ field5 │ {}    │\n",
+            "└────────┴───────┘",
+        )
+    );
+}
+
+#[test]
+fn parse_list_empty_entity_plain_0() {
+    let value = json!([{}, "field1", {}, "field2", {}]);
+
+    let table = parse(&value).with(Style::modern()).to_string();
+
+    assert_eq!(
+        table,
+        concat!(
+            "┌────┬────────┬────┬────────┬────┐\n",
+            "│ {} │ field1 │ {} │ field2 │ {} │\n",
+            "└────┴────────┴────┴────────┴────┘",
+        )
+    );
+}
+
+#[test]
+fn parse_list_empty_entity_plain_1() {
+    let value = json!([[], "field1", [], "field2", []]);
+
+    let table = parse(&value).with(Style::modern()).to_string();
+
+    assert_eq!(
+        table,
+        concat!(
+            "┌────┬────────┬────┬────────┬────┐\n",
+            "│ [] │ field1 │ [] │ field2 │ [] │\n",
+            "└────┴────────┴────┴────────┴────┘",
+        )
+    );
+}
+
+#[test]
+fn parse_list_empty_entity_plain_2() {
+    let value = json!([{}, {}, {}]);
+
+    let table = parse(&value).with(Style::modern()).to_string();
+
+    assert_eq!(
+        table,
+        concat!(
+            "┌────┬────┬────┐\n",
+            "│ {} │ {} │ {} │\n",
+            "└────┴────┴────┘",
+        )
+    );
+}