I'm new to JSON. I have a dictionary, I convert it to a JSON object using
myJsonObject = json.dumps(myDictionary)
But then If I check the type, I get <type 'str'>
Is string is the expected output for a JSON object?
PS: Python 2.7
Update: How can I get a JSON type, if dumps is not the correct way?
If you have a Python object, you can convert it into a JSON string by using the json.dumps() method.
JSON (JavaScript Object Notation) is a popular data format used for representing structured data. It's common to transmit and receive data between a server and web application in JSON format. In Python, JSON exists as a string.
Python json module has a JSONEncoder class. You can extend it If you want more customized output. i.e., you will have to subclass JSONEncoder so you can implement your custom JSON serialization.
Of course, this is called "Serialization" - you are dumping a Python data structure into a JSON formatted string:
json.dumps()
Serialize obj to a JSON formatted str using this conversion table.
If you load a JSON string with loads()
, this would be "deserialization" and you would get a Python list or a dictionary.
The N of JSON is for Notation ; it is, indeed, a data format specification. A JSON object is usually intended to be used as a storage or inter-process communication format. That's why it is in Python the universal flat format accepted by write() : a string object.
You can print it to screen, save it to a file, or communicate it to another program to verify it is correct and containing expected data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With