Handling json with python

 Json was originally a structure to describe an object in Javascript, but now it is widely used to describe a universal data structure. In particular, it is widely used as a data protocol for sending and receiving data in http, tcp/ip communication.

Working with Json in Python is as easy as Javascript.


Basic JSON handling

To handle JSON in Python, import the json module. The json module is provided by default in Python, so there is no need to install it using pip.


Read json from file

This is a method of reading json data from a file that stores json data. 

After importing the json module, you can import data through the load function.

import json

filename = "C:\\lsh\\study\\json\\drink.json"
with open(filename, 'r') as f:
    json_data = json.load(f)

print(json.dumps(json_data) )

Run the program, yes, I can get the good result.


For reference, the json.dumps function used for screen output converts json data in dictionary format to str format.

The important functions in the above example are as follows.
  • json.load : Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python dictionary object.
  • json.dumps : Serialize obj to a JSON formatted str

Handling the utf-8 charset

But if your json file contains unicode characters like this.

{
	"Coffee": {
		"price": "5000"
	},
	"수정과": {
		"price": "6000"
	},
	"Black Tea": {
		"price": "5500"
	}
}

You may get error like this.

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-5-142bddd324e7> in <module>
      3 filename = "C:\\lsh\\study\\json\\drink.json"
      4 with open(filename, 'r') as f:
----> 5     json_data = json.load(f)
      6 
      7 print(json.dumps(json_data) )

~\anaconda3\envs\py_3_8_11\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    291     kwarg; otherwise ``JSONDecoder`` is used.
    292     """
--> 293     return loads(fp.read(),
    294         cls=cls, object_hook=object_hook,
    295         parse_float=parse_float, parse_int=parse_int,

UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 43: illegal multibyte sequence

To avoid this error, always manage the charset of the json file as utf-8, not ascii. Also, add a parameter to use the utf-8 character set in the open function that opens a file. In the json.dumps function, add a parameter to use utf-8.

import json

filename = "C:\\lsh\\study\\json\\drink.json"
with open(filename, 'r', encoding="UTF-8") as f:
    json_data = json.load(f)

print(json.dumps(json_data, ensure_ascii=False) )


Run the program again, and there will be no more errors.

{"Coffee": {"price": "5000"}, "수정과": {"price": "6000"}, "Black Tea": {"price": "5500"}}

It is always a good idea to use the utf-8 character set when dealing with JSON files. Therefore, always save the json file in the utf-8 character set and add the encoding="UTF-8" parameter to the parameter of the open function. Also, use the ensure_ascii=False parameter in the json.dumps function, which converts the json data managed as a dictionary object into a string.


Output JSON data more beautifully

The json.dumps function is a function that converts json data into a string. This function has an indent option that makes the string easier to read.

import json

filename = "C:\\lsh\\study\\json\\drink.json"
with open(filename, 'r', encoding="UTF-8") as f:
    json_data = json.load(f)

print(json.dumps(json_data, ensure_ascii=False, indent='\t') )


It can be seen that the readability is much improved.

{
	"Coffee": {
		"price": "5000"
	},
	"수정과": {
		"price": "6000"
	},
	"Black Tea": {
		"price": "5500"
	}
}


Modify JSON data

Now that you have practiced reading JSON data from a file, let's practice writing JSON data to a file. Let's add a drink to the data in the JSON file we read earlier and then save it.

Note that the JSON data we are dealing with has a nested structure. So I used json_data["Coke"] = {} to create a nested dictionary and then put the values in it. If it is not a nested structure, you can use it like json_data["Coke"] = "3500".

json_data["Coke"] = {}
json_data["Coke"]["price"] = '3500' 
print(json.dumps(json_data, ensure_ascii=False, indent='\t') )

You can see that Coke is normally added to the json data.

{
	"Coffee": {
		"price": "5000"
	},
	"수정과": {
		"price": "6000"
	},
	"Black Tea": {
		"price": "5500"
	},
	"Coke": {
		"price": "3500"
	}
}

The important things in the above example are:

  • In Python, JSON data is treated as a dictionary object. So, if you have knowledge of how to manipulate dictionaries, you can easily change JSON data.


Saving JSON data to a file

It is very similar to reading json data from a file. The 'w' option of the open function creates a new file for writing. Write the changed JSON data to a file, and then read the file again to check the changes.

with open(filename, 'w', encoding='utf-8') as f:
    json.dump(json_data, f, ensure_ascii=False, indent="\t")

#Now reload the file to check the changes
with open(filename, 'r', encoding="UTF-8") as f:
    json_data = json.load(f)

print(json.dumps(json_data, ensure_ascii=False, indent='\t') )   


You can see that the changes have been properly written to the file.

{
	"Coffee": {
		"price": "5000"
	},
	"수정과": {
		"price": "6000"
	},
	"Black Tea": {
		"price": "5500"
	},
	"Coke": {
		"price": "3500"
	}
}

The important things in the above example are:

  • In Python, The function that writes JSON data to a file is the json.dump function.


Read json from string

Sometimes it is necessary to process json data in string format by making it into a Python dictionary object.  When reading json data from a file, we used the json.load function. To read json data from a string in memory, use the json.loads function.

drink = """
{
	"Coffee": {
		"price": "5000"
	},
	"수정과": {
		"price": "6000"
	},
	"Black Tea": {
		"price": "5500"
	},
	"Coke": {
		"price": "3500"
	}
}
"""

json_data = json.loads(drink)
print(json.dumps(json_data, ensure_ascii=False, indent='\t') ) 

You will get the same result as the previous examples. In Python 3, strings are treated as UTF-8, so there is no need to process an option such as encoding="UTF-8" as in the case of the function open that opens a file.


The important things in the above example are:

  • In Python, The function that reads JSON data from string is the json.dumps function.


Advanced JSON handling

This is not about JSON, but rather how to deal with Python dictionaries. This is because JSON data nested in multiple steps must also be managed as a nested dictionary object.


Nested Jason

First, create the following nested JSON:

import json

drink = """
{
    "Hot":{
        "Hot Americano": {
            "amount": "500ml",
            "price": "5000"
        },
        "cappuccino": {
            "amount": "300ml",
            "price": "5500"
        }
    },
    "Cold":{
        "Ice Americano": {
            "amount": "600ml",
            "price": "5500"
        },
        "Coke": {
            "amount": "600ml",
            "price": "3500"
        }
    }
}

"""

json_data = json.loads(drink)
print(json.dumps(json_data, ensure_ascii=False, indent='\t') ) 


As mentioned earlier, JSON data is treated as a dictionary in Python.  Therefore, if you want to know the price of the "Hot Amecicano" menu, you can access it as follows.


If you want to change the amount of "Hot Americano"




If you want to add "Black Tea" to the "Hot" category:



List in the Jason 

As in the following example, a list can come in the dictionary value of json.

drink = """
{
    "Hot":{
        "Hot Americano": [{"amount": "500ml"}, {"price": "5000"}]
        ,
        "cappuccino": [{"amount": "300ml"}, {"price": "5500" }]
    },
    "Cold":{
        "Ice Americano": [{"amount": "600ml", "price": "5500"}],
        "Coke": [{"amount": "600ml", "price": "3500"}]
        }
}
"""
json_data = json.loads(drink)
print(json.dumps(json_data, ensure_ascii=False, indent='\t') ) 

The result is like this.

{
	"Hot": {
		"Hot Americano": [
			{
				"amount": "500ml"
			},
			{
				"price": "5000"
			}
		],
		"cappuccino": [
			{
				"amount": "300ml"
			},
			{
				"price": "5500"
			}
		]
	},
	"Cold": {
		"Ice Americano": [
			{
				"amount": "600ml",
				"price": "5500"
			}
		],
		"Coke": [
			{
				"amount": "600ml",
				"price": "3500"
			}
		]
	}
}


Note that the json of Hot and Cold categories is slightly different. Hot has two dictionary elements in list, but Cold contains one dictionary element.




If you want to change the cappuccino and Coke prices:




The result is like this.

{
	"Hot": {
		"Hot Americano": [
			{
				"amount": "500ml"
			},
			{
				"price": "5000"
			}
		],
		"cappuccino": [
			{
				"amount": "300ml"
			},
			{
			    "price": "5501"
			}
		]
	},
	"Cold": {
		"Ice Americano": [
			{
				"amount": "600ml",
				"price": "5500"
			}
		],
		"Coke": [
			{
				"amount": "600ml",
				"price": "3501"
			}
		]
	}
}


If you want to add a new Cold menu:


The result is like this.

{
	"Hot": {
		"Hot Americano": [
			{
				"amount": "500ml"
			},
			{
				"price": "5000"
			}
		],
		"cappuccino": [
			{
				"amount": "300ml"
			},
			{
				"price": "5501"
			}
		]
	},
	"Cold": {
		"Ice Americano": [
			{
				"amount": "600ml",
				"price": "5500"
			}
		],
		"Coke": [
			{
				"amount": "600ml",
				"price": "3501"
			}
		],
		"Sprite": [
			{
				"price": "5400",
				"amount": "350 ml"
			}
		]
	}
}


If you want to add a new Hot menu:


The result is like this.

{
	"Hot": {
		"Hot Americano": [
			{
				"amount": "500ml"
			},
			{
				"price": "5000"
			}
		],
		"cappuccino": [
			{
				"amount": "300ml"
			},
			{
				"price": "5501"
			}
		],
		"Red Tea": [
			{
				"price": "5400"
			},
			{
				"amount": "350 ml"
			}
		]
	},
	"Cold": {
		"Ice Americano": [
			{
				"amount": "600ml",
				"price": "5500"
			}
		],
		"Coke": [
			{
				"amount": "600ml",
				"price": "3501"
			}
		],
		"Sprite": [
			{
				"price": "5400",
				"amount": "350 ml"
			}
		]
	}
}


























댓글

이 블로그의 인기 게시물

MQTT - C/C++ Client

RabbitMQ - C++ Client #1 : Installing C/C++ Libraries

C/C++ - Everything about time, date