tshepang
tshepang

Reputation: 12459

Decoding JSON-like object using the json module

I want to be able to decode a format that looks like JSON, but can handle hexadecimal content. Let's call it JSHON. Example content:

{
    "nine": 9,
    "ten":  0xA,
    "eleven": 11
}

Is it easy to coax the stdlib json module to provide such an ability, or should I use something else.

Upvotes: 1

Views: 109

Answers (2)

tshepang
tshepang

Reputation: 12459

According to its documentation, demjson supports this (in its non-strict mode). It has no support for Python 3 yet, but it will do for now.

Upvotes: 0

Jared
Jared

Reputation: 26397

Simple fix would be to use ast.literal_eval,

>>> data = '''
... {
...     "nine": 9,
...     "ten":  0xA,
...     "eleven": 11
... }
... '''
>>> import ast
>>> ast.literal_eval(data)
{'eleven': 11, 'nine': 9, 'ten': 10}

Upvotes: 3

Related Questions