1. /
  2. channels
  3. /
  4. mojo-community-nightly
  5. /
  6. emberjson
mojo-community-nightly
public
10
Package icon
emberjson
v0.1.2.dev2025021118
A json library for Mojo

Install with Pixi

pixi add emberjson

Install with conda

conda install -c https://prefix.dev/mojo-community-nightly emberjson

Description

EmberJson

A lightweight JSON parsing library for Mojo.

Usage

Parsing JSON

Use the parse function to parse a JSON value from a string. It accepts a ParseOptions struct as a parameter to alter parsing behaviour.


from emberjson import parse

struct ParseOptions:
    # Always use the fast past during float point value parsing.
    # Use this only if you are comfortable with potentially reduced accuracy.
    var fast_float_parsing: Bool

...

var json = parse[ParseOptions(fast_float_parsing=True)]('{"key": 123}')

EmberJSON supports decoding escaped unicode characters.

print(parse(r'["\uD83D\uDD25"]')) # prints '["πŸ”₯"]'

Converting to String

Use the to_string function to convert a JSON struct to its string representation. It accepts a parameter to control whether to pretty print the value. The JSON struct also conforms to the Stringable, Representable and Writable traits.

from emberjson import to_string

var json = parse('{"key": 123}')

print(to_string(json)) # prints {"key":123}
print(to_string[pretty=True](json))
# prints:
#{
#   "key": 123
#}

Working with JSON

JSON is the top level type for a document. It can contain either an Object or Array.

Value is used to wrap the various possible primitives that an object or array can contain, which are Int, Float64, String, Bool, Object, Array, and Null.

from emberjson import *

var json = parse('{"key": 123}')

# check inner type
print(json.is_object()) # prints True

# dict style access
print(json.object()["key"].int()) # prints 123

# array
var array = parse('[123, 4.5, "string", True, null]').array()

# array style access
print(array[3].bool()) # prints True

# equality checks
print(array[4] == Null()) # prints True

# None converts implicitly to Null
assert_equal(array[4], Value(None))

# Implicit ctors for Value
var v: Value = "some string"

# Convert Array and Dict back to stdlib types
# These are consuming actions so the original Array/Object will be moved
var arr = Array(123, False)
var l = arr.to_list()

var ob = Object()
var d = ob.to_dict()

Download counts

Loading chart data...

Version

0.1.2.dev2025021118

License

Platforms

linux-64
osx-arm64

Last Published

8 months ago
Package Variants
Version
Build
Created
Size
Architecture
Downloads
emberjson-0.1.2.dev2025021118-hb0f4dca_0.conda
0.1.2.dev2025021118
hb0f4dca_0 (0)
8 months ago
822.38 KB
linux-64
N/A
emberjson-0.1.2.dev2025021118-h60d57d3_0.conda
0.1.2.dev2025021118
h60d57d3_0 (0)
8 months ago
822.33 KB
osx-arm64
N/A
emberjson-0.1.2.dev2025020605-hb0f4dca_0.conda
0.1.2.dev2025020605
hb0f4dca_0 (0)
8 months ago
821.02 KB
linux-64
N/A
emberjson-0.1.2.dev2025020605-h60d57d3_0.conda
0.1.2.dev2025020605
h60d57d3_0 (0)
8 months ago
821.07 KB
osx-arm64
N/A
emberjson-0.1.2.dev2025020316-hb0f4dca_0.conda
0.1.2.dev2025020316
hb0f4dca_0 (0)
8 months ago
809.08 KB
linux-64
N/A
emberjson-0.1.2.dev2025020316-h60d57d3_0.conda
0.1.2.dev2025020316
h60d57d3_0 (0)
8 months ago
809.07 KB
osx-arm64
N/A
emberjson-0.1.2.dev2025013105-hb0f4dca_0.conda
0.1.2.dev2025013105
hb0f4dca_0 (0)
8 months ago
829.39 KB
linux-64
N/A
emberjson-0.1.2.dev2025013105-h60d57d3_0.conda
0.1.2.dev2025013105
h60d57d3_0 (0)
8 months ago
829.38 KB
osx-arm64
N/A
emberjson-0.1.2.dev2025012705-hb0f4dca_0.conda
0.1.2.dev2025012705
hb0f4dca_0 (0)
8 months ago
823.86 KB
linux-64
N/A
emberjson-0.1.2.dev2025012705-h60d57d3_0.conda
0.1.2.dev2025012705
h60d57d3_0 (0)
8 months ago
823.83 KB
osx-arm64
N/A