Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of a JSON object in python?

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?

like image 537
KhaledMaged Avatar asked Jun 07 '16 15:06

KhaledMaged


People also ask

Is there type JSON in Python?

If you have a Python object, you can convert it into a JSON string by using the json.dumps() method.

What is object JSON () in Python?

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.

Is JSON a class in Python?

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.


2 Answers

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.

like image 148
alecxe Avatar answered Sep 20 '22 15:09

alecxe


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.

like image 42
Arthur Hv Avatar answered Sep 20 '22 15:09

Arthur Hv