简体   繁体   中英

Extract a string value of a key into another key/value in nested json and insert onto sql db

Extract data from a nested json file and insert to sql db. From key details I want to get information below and insert onto sql db;

sample of json file data and key/value as follows

{
  "response": {
   "client_log": {
      "data": [
        {
          "city": "LONDON",
          "login": "AAAAAAAAAAAAAA",
          "state": "MC",
          "details": "Please find report below:\r\n\r\n------Report Information------\r\n\r\nEmail Id: user1@gmail.com\r\nServ Id: 1101ar12\r\nServ Num: 11111\r\nServ Details: Super-A\r\nState: LONDON\r\nCity: LONDON\r\n\r\n------Service Information------\r\n\r\nUser Name: John Clark\r\nMobile Number: 000111222\r\n\r\n------Reported Form------\r\n\r\nForm-1: zzzzz\r\nType: 111\r\n\r\nRemarks: Remarks 123.",
          "log_number": "1",
          "department": "Sales",
          "staff_id": "S123",
          "staff_name": "EricY",
          "timestamp": "2020-02-27 15:57:24"
        },
        {
          "city": "SINGAPORE",
          "login": "BBBBBBBBBBBBB",
          "state": "XX",
          "details": "Please find report below:\r\n\r\n------Report Information------\r\n\r\nEmail Id: user2@gmail.com\r\nServ Id: 903oa112\r\nServ Num: 12345\r\nServ Details: Super-B\r\nState: Sydney\r\nCity: Sydney\r\n\r\n------Service Information------\r\n\r\nUser Name: Peter\r\nMobile Number: 333444555\r\n\r\n------Reported Form------\r\n\r\nForm-2: xxxxxxxxxx\r\nType: 111\r\n\r\nRemarks: Remarks 890.",
          "log_number": "1",
          "department": "Eng",
          "staff_id": "S456",
          "staff_name": "YongG",
          "timestamp": "2020-02-27 15:57:24"
        }
      ],
      "query": "13"
    },
    "response_time": "0.723494",
    "transaction_id": "909122",
    "transaction_status": "OK",

  }
}

This is snippet code I use to extract the data and insert onto sql

myfile = 'sample.json'

with open(myfile, 'r') as f:
   mydata = json.load(f)


sql = "INSERT INTO `table1` (`city`, `login`, `state`, `details`, `log_number`, `department`, `staff_id`, `staff_name`, `timestamp`) VALUES ( %(city)s, %(login)s, %(state)s, %(details)s, %(log_number)s, %(department)s, %(staff_id)s, %(staff_name)s, %(timestamp)s )"
cursor.executemany( sql, mydata['response']['client_log']['data'])

db.commit()
db.close()

from the code above I'm able to get the data inclusive key details but the details value I got is presented and inserted on sql in 1 big chunk data (sql column details)... Thank you.

You can easily parse the str associated with the details key using the split() and/or find() methods. Go line by line, and assume everything to the left of the : is the key, and everything to the right is the value.

For example:

import json

myfile = 'sample.json'

with open(myfile, 'r') as f:
   mydata = json.load(f)

for entry in mydata['response']['client_log']['data']:
    parsed_details = {}
    for line in entry['details'].split('\r\n'):
        split = line.find(': ') # find() returns -1 if no match is found
        if split != -1:
            key = line[:split]
            value = line[split+2:] # 2 = len(': ')
            parsed_details[key] = value
    entry['parsed_details'] = parsed_details

or

import json

myfile = 'sample.json'

with open(myfile, 'r') as f:
   mydata = json.load(f)

for entry in mydata['response']['client_log']['data']:
    parsed_details = {}
    for line in entry['details'].split('\r\n'):
        try:
            key, value = line.split(': ', maxsplit=1)
            parsed_details[key] = value
        except ValueError:
            # This error is only thrown when the line doesn't have ': ' in it, 
            # which means there aren't enough values to unpack. It is safe to pass.
            pass
    entry['parsed_details'] = parsed_details

The dict that is each item of the mydata['response']['client_log']['data'] list now has a parsed_details key, whose value is a dict with key and values pairs extracted from the details key, as you can see in this output:

mydata
Out[2]: 
{'response': {'client_log': {'data': [{'city': 'LONDON',
     'login': 'AAAAAAAAAAAAAA',
     'state': 'MC',
     'details': 'Please find report below:\r\n\r\n------Report Information------\r\n\r\nEmail Id: user1@gmail.com\r\nServ Id: 1101ar12\r\nServ Num: 11111\r\nServ Details: Super-A\r\nState: LONDON\r\nCity: LONDON\r\n\r\n------Service Information------\r\n\r\nUser Name: John Clark\r\nMobile Number: 000111222\r\n\r\n------Reported Form------\r\n\r\nForm-1: zzzzz\r\nType: 111\r\n\r\nRemarks: Remarks 123.',
     'log_number': '1',
     'department': 'Sales',
     'staff_id': 'S123',
     'staff_name': 'EricY',
     'timestamp': '2020-02-27 15:57:24',
     'parsed_details': {'Email Id': 'user1@gmail.com',
      'Serv Id': '1101ar12',
      'Serv Num': '11111',
      'Serv Details': 'Super-A',
      'State': 'LONDON',
      'City': 'LONDON',
      'User Name': 'John Clark',
      'Mobile Number': '000111222',
      'Form-1': 'zzzzz',
      'Type': '111',
      'Remarks': 'Remarks 123.'}},
    {'city': 'SINGAPORE',
     'login': 'BBBBBBBBBBBBB',
     'state': 'XX',
     'details': 'Please find report below:\r\n\r\n------Report Information------\r\n\r\nEmail Id: user2@gmail.com\r\nServ Id: 903oa112\r\nServ Num: 12345\r\nServ Details: Super-B\r\nState: Sydney\r\nCity: Sydney\r\n\r\n------Service Information------\r\n\r\nUser Name: Peter\r\nMobile Number: 333444555\r\n\r\n------Reported Form------\r\n\r\nForm-2: xxxxxxxxxx\r\nType: 111\r\n\r\nRemarks: Remarks 890.',
     'log_number': '1',
     'department': 'Eng',
     'staff_id': 'S456',
     'staff_name': 'YongG',
     'timestamp': '2020-02-27 15:57:24',
     'parsed_details': {'Email Id': 'user2@gmail.com',
      'Serv Id': '903oa112',
      'Serv Num': '12345',
      'Serv Details': 'Super-B',
      'State': 'Sydney',
      'City': 'Sydney',
      'User Name': 'Peter',
      'Mobile Number': '333444555',
      'Form-2': 'xxxxxxxxxx',
      'Type': '111',
      'Remarks': 'Remarks 890.'}}],
   'query': '13'},
  'response_time': '0.723494',
  'transaction_id': '909122',
  'transaction_status': 'OK'}}

I'm not very familiar with SQL, so I can't help you with your last step of updating your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM