Python JSON



Python JSONBefore we get started with how to use JSON with Python, we’ll take a look at “What is JSON in the first place?”JSONThe JavaScript Object Notation – JSON, is a subset of Javascript and it is something like an interpreter/translator that makes it super easy for both humans and machines to read data!It’s much more compact / lightweight compared to other file formats like XML.Note: ‘YAML’ is a superset of JSON and it allows users to comment while the same does not hold true for JSON.In short, JSON is a web parser that reduces the size of data or to be precise, converts them into lightweight format.Alright, that’s enough introduction about JSON. Let’s see why and where do we use it.Why JSON?The reason is simple. If there are better alternatives to XML, why not go for it? There is one more reason people prefer JSON. The syntax and semantics are very similar to that of Python and C. We’ll come to that asap.Where do we need JSON?This can be pretty much used in situations where we need to store and transmit ‘structured’ data. Most commonly used in web applications to send the data from your server to the web browser.Now that we know the ‘what, why, where’ of JSON, let’s take a look at some examples to know more about how to use it. For this, we’ll be using python programming language.EXAMPLES:Before we jump into the example, there are few things to know about JSON and its syntax. Look at the following example…JSON data{ "Name": "Alex", "Pet name": "Al", "Sports": ["Athlete", "Swimmer", "Football player"], "age": 22, "Parent": [ { "Name": "Alice", "age": 44},{"Name": "Jason", "age": 48} ]}Does this Syntax look Familiar? You are right, JSON is very similar to Python Dictionaries. The only difference we need to make is to pass it as a string. So now its clear that the data is in NAME : VALUE pair which is similar to dictionary’s KEY : VALUE pair. Now we know how the JSON data looks like, let’s see how to use them with Python.Note: Python has a built-in package called?json to work with JSON dataGood. Now before proceeding to the next step, there’s one more thing we need to know. That is Encoding (referred to as ‘Serialization’) and Decoding (referred to as ‘Deserialization’) Let’s try to understand this in a more elaborated manner.We use ‘Serialization’ to convert Python objects to JSON accordingly. When encoding the Python objects, it gets converted to the JSON equivalent.PythonJSONdictobjectlist, tuplearraystrstringint, long, floatnumberNonenullOkay, enough words. Let’s get into the program to convert JSON to Python (i.e) Decoding – ‘deserialization’Remember we need pass the data as a string.The json.loads() will help us parse the json string.Congrats on parsing your ‘data’.During Decoding, Source: let’s try converting Python to JSON (i.e) Encode – ‘Serialization’O/P:Just as we did for deserialization, instead of json.loads(), we use json.dumps() to convert the python dictionary into JSON format.You can also do sorting. Let’s play with it a bit.Pass ‘sort_keys=True’ as a parameter to sort the dictionary.Let’s try indenting now. Keypoints :json.loads(data) it takes a string, bytes, or byte array instance which contains the JSON and returns an python object.json.dumps(data) it takes a string, bytes, or byte array instance which contains the JSON and returns an python object.During Dumping, you can pass the inputs as lists, dictionaries, strings and many more python objects. So feel free to play around with them. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download