【问题标题】:Issue importing JSON object into a MySQL column将 JSON 对象导入 MySQL 列的问题
【发布时间】:2021-06-27 14:51:34
【问题描述】:

我创建了一个 Python 脚本,它在 MySQL 中创建一个表,另一个用 JSON 文件中的数据填充它。

示例 JSON 文件:

{
  "ansible_facts":{
    "ansible_network_resources":{
      "l3_interfaces":[
        {
          "name":"GigabitEthernet0/0"
        },
        {
          "name":"GigabitEthernet0/0.100",
          "ipv4":[
            {
              "address":"172.1.1.1 255.255.255.252"
            }
          ]
        },
        {
          "name":"GigabitEthernet0/0.101",
          "ipv4":[
            {
              "address":"172.1.1.1 255.255.255.252"
            }
          ]
        },
        {
          "name":"GigabitEthernet0/1",
          "ipv4":[
            {
              "address":"56.2.1.1 255.255.255.252"
            }
          ]
        },
        {
          "name":"GigabitEthernet0/2"
        }
      ]
    },
    "ansible_net_python_version":"3.6.9",
    "ansible_net_hostname":"host02342-mpls",
    "ansible_net_model":"CISCO-CHA",
    "ansible_net_serialnum":"F1539AM",
    "ansible_net_gather_subset":[
      "default"
    ],
    "ansible_net_gather_network_resources":[
      "l3_interfaces"
    ],
    "ansible_net_version":"15.3(2)T",
    "ansible_net_api":"cliconf",
    "ansible_net_system":"ios",
    "ansible_net_image":"flash0:/c3900-universalk9-mz.spa.153-2.t.bin",
    "ansible_net_iostype":"IOS"
  }
}

表创建脚本

import mysql.connector

mydb = mysql.connector.connect(host="IPaddress", user="user", password="pw", database="db")
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE Routers (ansible_net_hostname NVARCHAR(255), ansible_net_model NVARCHAR(255), ansible_network_resources NVARCHAR(255))")

将 JSON 数据导入 MySQL 的脚本

import json, pymysql

json_data = open("L3_out.json").read()
json_obj = json.loads(json_data)
con = pymysql.connect(host="IPaddress", user="user", password="pw", database="db")

cursor = con.cursor()
for item in json_obj:
    ansible_net_hostname = item.get("ansible_net_hostname")
    ansible_net_model = item.get("ansible_net_model")
    ansible_network_resources = item.get("ansible_network_resources")
    cursor.execute(
        "insert into Routers(ansible_net_hostname, ansible_net_model, ansible_network_resources) value(%s, %s, %s)",
        (ansible_net_hostname, ansible_net_model, ansible_network_resources)
con.commit()
con.close()

我在将ansible_network_resources 字段对象导入Routers 表时遇到问题。其他列(ansible_net_hostnameansible_net_model)被完美插入。我做错了什么?

【问题讨论】:

  • 旁注:您缺少右圆括号 - ) - 以关闭第二个 Python 脚本中的 cursor.execute() 调用。

标签: python mysql json


【解决方案1】:

首先不清楚是怎么做的

for item in json_obj:
    ansible_net_hostname=item.get("ansible_net_hostname")

工作。 由于您的情况下的“项目”是字典中的一个键。在您显示的文件中,只有一个根键“ansible_facts”。所以你试图在字符串上调用 get() 。 要获取“ansible_network_resources”的数据,请执行以下操作:

for key in json_obj:
    ansible_network_resources=json_obj[key].get("ansible_network_resources")

【讨论】:

  • 先生,使用“key”而不是“item”时出现错误
  • 错误:回溯(最近一次调用最后):文件“json-to-mysql.py”,第 9 行,在 ansible_network_resources=json_obj[key].get("ansible_network_resources") TypeError : 列表索引必须是整数或切片,而不是字典
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 2012-04-03
  • 2020-10-06
  • 2012-08-06
相关资源
最近更新 更多