Sitemap
Nishaanth S P

Nishaanth S P

Press enter or click to view image in full size

Python — EDN to JSON Tool

4 min readDec 19, 2024

--

This is a follow-up to my article Python — Convert File Types Using Streamlit. As a Clojure developer, I often face the challenge of converting data between JSON and EDN formats. Tools like Postman require JSON, but Clojure uses EDN (Extensible Data Notation) for its data structures.

I’ve added a new page to the tool to make this easier. This feature lets you quickly convert JSON to EDN and EDN to JSON. It simplifies the workflow for testing APIs and working with data in both formats without needing separate scripts or tools.

Try the tool out from here

The Problem

Working with APIs often means switching between formats. While JSON is widely used in tools like Postman, Clojure developers rely on EDN for data handling. Manually converting these formats can be a repetitive and error-prone task, especially with large or complex datasets.

Get Nishaanth S P’s stories in your inbox

Join Medium for free to get updates from this writer.

For example, when testing an API response in Postman, I might get JSON output that needs to be converted into EDN to use in my Clojure code. Similarly, I might start with EDN data and need it in JSON format for testing or sharing. This back-and-forth conversion became frustrating, and I wanted an easier way to handle it.

The Solution

To solve this problem, I added a new page to the Streamlit tool. It now supports:

  • JSON to EDN conversion: Transform JSON data into EDN, making it ready for use in Clojure projects.
  • EDN to JSON conversion: Convert EDN data into JSON for use in tools like Postman or JavaScript applications.

This new page provides a simple interface for seamlessly handling both conversions. It saves time and effort, especially for developers working in a multi-language environment like mine.

The Core Logic

The new functionality for converting JSON to EDN and EDN to JSON is built using the edn_format library. This library makes it easy to parse and format EDN data in Python. Here’s a breakdown of how the conversions work:

Converting EDN to JSON

  1. Loading EDN Data: The EDN string is parsed into an object using edn_format.loads().
  2. Making it JSON-Serializable: A recursive function, __convert_to_json_serializable, ensures all EDN types (e.g., ImmutableDict, ImmutableList, Keyword, Symbol) are converted into equivalent JSON-compatible types (e.g., dictionaries, lists, strings).
  3. Formatting to JSON: The serialized object is formatted into JSON using Python’s json.dumps() for readability.

Here’s the key function for this conversion:

def ednToJSON(edn_string):
edn_obj = edn_format.loads(edn_string)
json_serialized = __convert_to_json_serializable(edn_obj)
return json.dumps(json_serialized, indent=4)

The __convert_to_json_serializable function handles:

  • Converting ImmutableDict to Python dict.
  • Converting ImmutableList to Python list.
  • Stripping : from EDN Keyword names and keys in maps.

Converting JSON to EDN

This process works in reverse:

  1. Parsing JSON Data: The JSON string is converted into a Python object using json.loads().
  2. Converting to EDN-Compatible Types: A recursive function, __convert_to_edn, ensures JSON types (e.g., dictionaries, lists, strings) are converted into EDN types (ImmutableDict, ImmutableList, Keyword).
  3. Formatting to EDN: The object is formatted back into EDN using edn_format.dumps().

Here’s the function for this conversion:

def jsonToEdn(json_string):
json_obj = json.loads(json_string)
edn_formatted = __convert_to_edn(json_obj)
return edn_format.dumps(edn_formatted, indent=4)

The __convert_to_edn function handles:

  • Converting Python dict to EDN ImmutableDict with keys as EDN Keywords.
  • Converting Python list to EDN ImmutableList.

Note the __convert_to_edn and __convert_to_json_serializable are recursive in nature to reach the depths of the EDN or JSON

A glance at the UX

Press enter or click to view image in full size

The conversion works seamlessly, and the frontend is kept minimal with just a text area, columns, and buttons.

That said, I have to confess — button alignment isn’t exactly Streamlit’s strongest suit. Since there’s no proper horizontal alignment option, I had to make it work with a workaround.

It looks fine in full-screen mode, but if you resize the window… well, let’s just say the buttons might show their true rebellious nature. Let’s call it “dynamic creativity”! 😅

Conclusion

With this new feature, the tool now supports converting JSON to EDN and vice versa, making it even more versatile for developers working across languages and frameworks. Streamlit once again proves to be an excellent choice for building quick, interactive tools that solve real problems.

On a lighter note, if I ever decide to turn this into a startup, the tagline is ready: “Converting files, saving lives!” 😄

--

--