遍历一个Json文件并在Python中插入db

时间:2019-03-07 16:18:54

标签: python mongodb list

{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]},
{"groupId":"org.springframework","artifactId":"spring-beans","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/5.1.2.RELEASE/5d513701a79c92f0549574f5170a05c4af7c893d/spring-beans-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-core","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.1.2.RELEASE/b9b00d4075c92761cfd4e527e0bdce1931b4f3dc/spring-core-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]},
{"groupId":"org.springframework","artifactId":"spring-expression","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-expression/5.1.2.RELEASE/3c16b062785e4c101db6b754fcb34a77c1e912c/spring-expression-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-core","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.1.2.RELEASE/b9b00d4075c92761cfd4e527e0bdce1931b4f3dc/spring-core-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]},
{"groupId":"org.springframework","artifactId":"spring-core","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.1.2.RELEASE/b9b00d4075c92761cfd4e527e0bdce1931b4f3dc/spring-core-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]},
{"groupId":"org.springframework","artifactId":"spring-web","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-web/5.1.2.RELEASE/3ff2a93b072da42c3930225e3dceeabb0678eb0b/spring-web-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-beans","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/5.1.2.RELEASE/5d513701a79c92f0549574f5170a05c4af7c893d/spring-beans-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-core","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.1.2.RELEASE/b9b00d4075c92761cfd4e527e0bdce1931b4f3dc/spring-core-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]},
{"groupId":"org.springframework","artifactId":"spring-core","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.1.2.RELEASE/b9b00d4075c92761cfd4e527e0bdce1931b4f3dc/spring-core-5.1.2.RELEASE.jar","dependencies":[
{"groupId":"org.springframework","artifactId":"spring-jcl","version":"5.1.2.RELEASE","file":"/home/howie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.1.2.RELEASE/f0d7165b6cfb90356da4f25b14a6437fdef1ec8a/spring-jcl-5.1.2.RELEASE.jar","dependencies":[]}]}]}]}]

output, error = process.communicate()
data = json.loads(output.decode('UTF-8'))

page = open("JSON/Java.json", "r")
parsed = json.loads(page.read())
for v in parsed['groupId'].values():
    if isinstance(v, dict):
        db_entry = {}
        db_entry['artifactId'] = v['artifactId']
        db_entry['version'] = v['version']
        db_entry['repo_id'] = repo_id
        dep_coll.insert_one(db_entry)
    else:
        raise ValueError("object is not dictionary")

嗨,我正在尝试在其上导入一个json文件,以便将某些值插入到数据库中。在这种情况下,我需要artifactid,插入版本。 json文件有一个列表,所以我将有多个插入。我想我走对了,任何帮助将不胜感激。目前,这是我在parsed ['groupId']。values()中获取v的错误: TypeError:列表索引必须是整数或切片,而不是str

1 个答案:

答案 0 :(得分:0)

'parsed'是一个列表,而不是字典。它包含字典。每个字典包含一个名为“ groupId”的字段。因此您的代码应如下所示。

如果以下代码不适合您,请将“ JSON / Java.json”上传到公共位置。

for entry in parsed:
    # entry is a dict - do something with it