anquegi / json-to-org-table

Converts JSON to linked org table

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

json-to-org-table

Overview

json-to-org-table is an elisp function to turn json strings org tables while maintaining the linkage of nested structures.

This was created to two primary use cases:

  • Human readability of API results
  • Exportablity of API results

Use

Use it by calling

(json-to-org-table-parse-json-string <some json string>)
(json-to-org-table-parse-json <Emacs json object>)

Example:

(json-to-org-table-parse-json-string "{\"glossary\": {\"title\": \"example glossary\",\"GlossDiv\": {\"title\": \"S\",\"GlossList\": {\"GlossEntry\": {\"ID\": \"SGML\",\"SortAs\": \"SGML\",\"GlossTerm\": \"Standard Generalized Markup Language\",\"Acronym\": \"SGML\",\"Abbrev\": \"ISO 8879:1986\",\"GlossDef\": {\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}")
| key      | value    |
|----------+----------|
| glossary | [[glossary]] |

#+name: glossary
| key      | value             |
|----------+-------------------|
| title    | example glossary  |
| GlossDiv | [[GlossDiv_glossary]] |

#+name: GlossDiv_glossary
| key       | value                       |
|-----------+-----------------------------|
| title     | S                           |
| GlossList | [[GlossList_GlossDiv_glossary]] |

#+name: GlossList_GlossDiv_glossary
| key        | value                                  |
|------------+----------------------------------------|
| GlossEntry | [[GlossEntry_GlossList_GlossDiv_glossary]] |

#+name: GlossEntry_GlossList_GlossDiv_glossary
| key       | value                                           |
|-----------+-------------------------------------------------|
| ID        | SGML                                            |
| SortAs    | SGML                                            |
| GlossTerm | Standard Generalized Markup Language            |
| Acronym   | SGML                                            |
| Abbrev    | ISO 8879:1986                                   |
| GlossDef  | [[GlossDef_GlossEntry_GlossList_GlossDiv_glossary]] |
| GlossSee  | markup                                          |

#+name: GlossDef_GlossEntry_GlossList_GlossDiv_glossary
| key          | value                                                                    |
|--------------+--------------------------------------------------------------------------|
| para         | A meta-markup language, used to create markup languages such as DocBook. |
| GlossSeeAlso | [[GlossSeeAlso_GlossDef_GlossEntry_GlossList_GlossDiv_glossary]]             |

#+name: GlossSeeAlso_GlossDef_GlossEntry_GlossList_GlossDiv_glossary
| GlossSeeAlso_GlossDef_GlossEntry_GlossList_GlossDiv_glossary |
|--------------------------------------------------------------|
| GML                                                          |
| XML                                                          |

Structures

Hashmap

Json

{"Celestial Body": "Luna",
 "Inhabitantants": "Loonies",
 "Government": null }

…becomes

| key            | value   |
|----------------+---------|
| Celestial Body | Luna    |
| Inhabitants    | Loonies |
| Government     |         |

List of Hashmaps

json

[{"type": "human", "name": "Manuel", "nickname": "Mannie"},
 {"type": "AI", "name": "HOLMES IV", "nickname": "Mike"},
 {"type": "human", "name": "Bernardo de la Paz", "nickname": "The Professor"}]

…becomes

| type  | name               | nickname      |
|-------+--------------------+---------------|
| human | Manuel             | Mannie        |
| AI    | HOLMES IV          | Mike          |
| human | Bernardo de la Paz | The Professor |

A List of Lists

json

[[3345246207 "launch" "hit"],
 [3345286207 "launch" "critical hit"],
 [3345946207 "launch" "hit"]]

…becomes

| 3345246207 | launch | hit          |
| 3345286207 | launch | critical hit |
| 3345946207 | launch | hit          |

A List of none of the above

json

["The Dinkum Thinkum", "A Rabble in Arms", "TANSTAAFL"]

…becomes

| The Dinkum Thinkum |
| A Rabble in Arms   |
| TANSTAAFL          |

The linkages are maintained between nested objects

json

{"genre": "Science Fiction",
 "author": "Robert Heinlein",
 "main-characters": ["Mannie",
                     "Wyoh",
                     "Professor Bernardo de la Paz",
                     "Mike",
                     "Stu",
                     "Hazel Meade"]}

…becomes

| genre           | Science Fiction |
| author          | Robert Heinlein |
| main-characters | [[characters]]  |

#+name: characters
| characters                   |
|------------------------------|
| Mannie                       |
| Wyoh                         |
| Professor Bernardo de la Paz |
| Mike                         |
| Stu                          |
| Hazel Meade                  |

WIP

Org bable post processing with json-to-org-table

#+name: requester
#+begin_src bash :results drawer
curl -XGET https://jsonplaceholder.typicode.com/posts/1
#+end_src

#+RESULTS: requester
:results:
{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
:end:

#+name: to-json-table
#+begin_src emacs-lisp :var str=requester() :results raw
(json-to-org-table-parse-json-string str)
#+end_src

#+RESULTS: to-json-table
| key    | value                                                                                                                                                       |
|--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| userId | 1                                                                                                                                                           |
| id     | 1                                                                                                                                                           |
| title  | sunt aut facere repellat provident occaecati excepturi optio reprehenderit                                                                                  |
| body   | quia et suscipitsuscipit recusandae consequuntur expedita et cumreprehenderit molestiae ut ut quas totamnostrum rerum est autem sunt rem eveniet architecto |

About

Converts JSON to linked org table


Languages

Language:Emacs Lisp 63.8%Language:Python 36.2%