1

i want to parse JSON to mysql database with python 3. Lets say i have JSON from www.test.com/api/students contains list of students,


{
    contents: [
        {
            id:"a",
            name:"rey"
        },
        {
            id:"b",
            name:"rio"
        },
        {
            id:"c",
            name:"ramy"
        },
    ]
}

and i have database called "student" and table MSStudent contains "student_id" and "student_name".

How to parse this JSON and pass it to my database, and how i query it into model on python?

thanks.

1 Answer 1

3

you can use the following methods:

Method 1: basic python

import json
json_data = json.loads("json string")
typ = type(json_data)
if typ == "dict":
    for key,value in json_data.items():
        print key+"=>"+val
        // insert in mysql (mysql query)
elif typ == "list":
    for k in json_data:
       print k

once you have parsed this, you may insert this in mysql. Method 2: use pandas

PS: this is just pseudo code, you need to modify it, create one function and parse every key and value, this has worked for me even for GB's of json data.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.