skip to Main Content

I am creating a JSON using Python. It looks something like this:

[
...
{"name":"John","is_active":true}
...
]

This is like a configuration JSON that I will be passing to a config file.
How can I insert true as a value.

I do not want ‘True’ or True

I want to insert lowercase true as a value.

2

Answers


  1. This automatically converts the True to true etc.

    import json
    
    jsonlist = [
        {"test": True},
        {"test": False},
    ]
    
    with open("test.json", "w") as f:
        json.dump(jsonlist, f)
    
    Login or Signup to reply.
  2. The json module automatically maps the Python value True to the JSON value true:

    For example,

    >>> json.dumps({"name": "John", "is_active": True})
    '{"name": "John", "is_active": true}'
    

    The converse is also true: the JSON value true is parsed as the Python value True:

    >>> json.loads('{"name": "John", "is_active": true}')
    {'name': 'John', 'is_active': True}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search