Saturday, September 8, 2018

Working With JSON in Python | Pandas Tutorial 6 | Big Data Tutorials | Codeing School

This is Pandas Tutorial Part 5. pandas is an open source, the BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

Working With JSON in Python | Pandas Tutorial 6 | Big Data Tutorials | Codeing School
Working With JSON in Python Pandas Tutorial 6 

Today’s Topic: How to Create or Work with JSON

JSON is a Python’s inbuilt module, no need to install this module.

**1st You need to import JSON module.

import json

Then take a son Data Set,

json_data =  '''
            {
                "source" : "this",
                "status" : "ok",
                "value" : "uid"
           
            }

'''

a = json.loads(json_data)

a

Output:
{'source': 'this', 'status': 'ok', 'value': 'uid'}

Logic: JSON is an inbuilt module of python. In JSON, there are not support commenting, in code.
In my code, json_data is a variable, in our normal python program ‘’’ is used for commenting code. But, when we use it with a variable, it became a part of the code. ‘a = json.loads(json_data)’ means, load the JSON into ‘a’ variable.

Print Particular Value:

a['source']

Output:
'this'

a['status']

Output:
'ok'

Logic: Here, a['status'] means, we passed the key value, and print the value against the key value. Here, ['status'] is a key value. See Img.

Working With JSON in Python | Pandas Tutorial 6 | Big Data Tutorials | Codeing School
JSON Code


Write the Data into a JSON File:

with open ('1.json', 'w') as f:
    json.dump(a, f)

Logic: Here, ‘1.json’ is the file name, where I want to write my data. ‘w’ means, I want to write this, so, we use ‘w’. ‘as f’ means, file format. ‘json.dump()’, this syntax dumps the JSON data in a JSON file. json.dump(a, f), here (a, f) means, ‘a’ is the variable where the JSON data will store, and ‘f’ is the file format.

Working With JSON in Python | Pandas Tutorial 6 | Big Data Tutorials | Codeing School
Write the JSON Data in a File


Here, is your JSON file.

Working With JSON in Python | Pandas Tutorial 6 | Big Data Tutorials | Codeing School
Your JSON File is Here


For More Information Read the Official JSON Documentation. Click Here.
  




If you have, any quarry please comment below. 

0 comments:

Post a Comment