简体   繁体   中英

iterate over a Json file and insert into db in Python

{"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")

hi i am trying to import a json file on iterate over it so that certain values are inserted into a db. In this case i need artifactid, version insertred. The json file has a list so i would have multiple inserts. I think i am on the right track , any help would be greatly appreciated. At the moment this is the error i am getting for v in parsed['groupId'].values(): TypeError: list indices must be integers or slices, not str

'parsed' is a list and not a dict. It contains dicts. Each dict contains a field named 'groupId'. So your code should look like.

Upload "JSON/Java.json" to a public location if the code below does not work for you.

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

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