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

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")


Leave a Comment

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.

Scroll to Top
%d bloggers like this:
Verified by MonsterInsights