Python Tutorial : Read data from Json and write to Files

Home/Programming Video Tutorials/Python Video Tutorials/Python Tutorial : Read data from Json and write to Files

Python Tutorial : Read data from Json and write to Files

FavoriteLoadingAdd to favorites

Reading data from Json using Python script is very easy. You can access records, fields and further use these in your application.

In this example, we are reading data from a json file using python script. After that, we create a text file for each record in json and write node values in these text files.

We have used json library of Python. Feel free to ask questions if you have any.

By | 2017-06-30T04:03:17+00:00 June 30th, 2017|Python Video Tutorials|15 Comments

15 Comments

  1. FilthyGreen June 30, 2017 at 4:03 am - Reply

    Very helpful, THANK YOU.

  2. fabval30 Newman June 30, 2017 at 4:07 am - Reply

    Please comment on this error

  3. unknown identity June 30, 2017 at 4:10 am - Reply

    Can we write data to json file by taking input from user ?
    waiting for your reply

  4. Eastrounds June 30, 2017 at 4:17 am - Reply

    Great tutorial! Subscribed to your channel.

  5. suria sarath June 30, 2017 at 4:18 am - Reply

    thanks for the video

  6. fabval30 Newman June 30, 2017 at 4:22 am - Reply

    ”’
    Json file has below conten:

    {
    "Employee": [{
    "name": "Raja",
    "dept": "IT"
    }, {
    "name": "Mohan",
    "dept": "Mech"
    }, {
    "name": "Gopal",
    "dept": "CSC"
    }]
    }

    ”’

    import json
    f = open(‘C:pythonPractisesample.json’)
    data = json.load(f)
    f.close()

    f=open(‘C:pythonPractisesample.txt’, ‘wb’)

    for item in data[‘Employee’]:
    f.write(item[‘name’]+’ ‘+item[‘dept’])
    f.close()

    ”’
    f.write(item[‘name’]+’ ‘+item[‘dept’])
    TypeError: a bytes-like object is required, not ‘str’
    ”’

  7. Christopher Joseph Spiteri June 30, 2017 at 4:26 am - Reply

    Great tutorial, cleared up a few things for me. Regards.

  8. Mali Bhaskaran June 30, 2017 at 4:30 am - Reply

    Thank you for this tutorial!!.. Very useful.

  9. Rex Morgan June 30, 2017 at 4:36 am - Reply

    Ignore my comment about the error. I am not sure what I did differently but now it works. Great video. Thanks.

  10. Akeem Coburn June 30, 2017 at 4:41 am - Reply

    Very nice. I’m learning python and had a similar project where I didn’t know how to print the individual persons. Thanks

  11. Janitha Prabhath June 30, 2017 at 4:47 am - Reply

    Thank You !!! It was really helpful

  12. Yash Parekh June 30, 2017 at 4:49 am - Reply

    share the code in the description

  13. Amber Sharma June 30, 2017 at 4:52 am - Reply

    Thank you for the wonderful video. I just had one doubt. You are creating the filenames based on the ids and names of the employees. that means you are dividing them based on their ids and names. What I am actually thinking is, I wanted to write files based on only names, and suppose there are 2 people with the same name for example "Kamal" repeats twice. I want both the Kamal’s details to be present in the same file. so I want the second Kamal’s details to get appended to the first Kamal’s file. I am trying to do this please help me out. If possible make a video on this. I have subscribed to your channel and will be waiting. Thank you 🙂

  14. arjun tv June 30, 2017 at 4:52 am - Reply

    i have converted a python data to json data… now i need to export that json data to file.. am a beginner.. pls help

  15. Rex Morgan June 30, 2017 at 4:53 am - Reply

    Good Day. I am having problems following along. I have some json data like in your example. See below:

    {
    "Employee":[
    {
    "name": "Kamal",
    "dept": "QA Center of Excellence",
    "id": "1",
    "manager": "Mr. X",
    "city": "New Delhi"
    },

    {
    "name": "Paul",
    "dept": "Prison",
    "id": "22",
    "manager": "Steve",
    "city": "New Orleans"
    },

    {
    "name": "Susan",
    "dept": "Human Resources",
    "id": "12",
    "manager": "Bob",
    "city": "Glasgow"
    },

    {
    "name": "Kamal",
    "dept": "Science",
    "id": "1",
    "manager": "Ted",
    "city": "Billings"
    },

    ]
    }

    my pthon code is:
    import json

    with open(‘input.json’) as json_data:
    data = json.load(json_data)
    print (data)

    my error is:
    Traceback (most recent call last):
    File "json_test2.py", line 5, in <module>
    data = json.load(json_data)
    File "C:Python27libjson__init__.py",
    **kw)
    File "C:Python27libjson__init__.py",
    return _default_decoder.decode(s)
    File "C:Python27libjsondecoder.py", l
    obj, end = self.raw_decode(s, idx=_w(s,
    File "C:Python27libjsondecoder.py", l
    raise ValueError("No JSON object could
    ValueError: No JSON object could be decoded

Leave A Comment

*