Categories
Python

Handling Json Strings in Python

In today’s post, we are going to explain how to handle Jason Strings in Python. There is JSON module in python which is used for JSON string handling. Jason is very common in RESTfull APIs. JSON is the abbreviation of JavaScript Object Notation.

Here is Tutorial about Python Dictionaries. We use Dictionary data structure for handling Key Value Pair type of Data.

 

Creating Dictionary Object

The simplest form of dictionary object is an empty dictionary which could be declared in python like this

empty = {}

These body brackets denote that the object is dictionary type of variable. Another example of dictionary data for Embedded developers could be the GPS coordinates. Let say we want to handle some structured data. We had already created Post of GPS Location Parsing in Raspberry PI Python. You can check out that link.

let’s say we had example coordinates like this

cord={"lat":"33.203942","Longitude": "23.0230920"}

So we will handle this type of structure using Dictionary Object. Above “cord” variable is dictionary variable.

Convert Dictionary to JSON String

To convert this variable into JSON we can use following code

import json
cord={"lat":"33.203942","Longitude": "23.0230920"}
json_data = json.dumps(cord)     #convert to valid json string
print(json_data)
json_obj=json.loads(json.dumps(cord))

Finding some Key in JSON Object

You can find if some specific key exists in JSON object or not using the following line of code

>>> if "lat" in json_obj:
        print("Key found")

 

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.