简体   繁体   English

使用 ZA7F517F354216B63827 将 API 生成的嵌套 Json 数据存储到 SQL DB 中的最佳方法是什么

[英]What is the best way to store a nested Json data generated from API into SQL DB using Python

I have a Json response coming from the API, i need to store this into a SQL DB from python.我有一个来自 API 的 Json 响应,我需要将它存储到来自 Z23EEEB7347BDD256BDDFC 的 SQL DB 中。 Apparently the Json data is nested.显然 Json 数据是嵌套的。 What is the best way to store this?最好的存储方法是什么?

Is there a way i can create table automatically with the schema that is generating by Json response, like it needs to take columns names from Json response and needs to be quite dynamic that if in future if there is a new value coming in it needs to create a new column有没有一种方法可以使用由 Json 响应生成的模式自动创建表,就像它需要从 Json 响应中获取列名,并且需要非常动态,如果将来有新值进入它需要创建一个新列

Here is the Json data example -这是 Json 数据示例 -

{
  "data": [
    {
      "id": 111,
      "name": "Test",
      "client": {
        "id": 12,
        "name": "Test"
      },
      "office": {
        "id": 12,
        "name": "Test",
        "country": "Test"
      },
      "primaryContact": {
        "name": "<PrimaryContactName>",
        "email": "<PrimaryContactEmail>"
      },
      "data": [
        {
          "fieldid": 10,
          "fieldLabel": "Test1",
          "hasMutlipleValues": false,
          "multiValues": [],
          "fieldValue": "Test2",
          "sfid": 0
        },
        {
          "fieldid": 132,
          "fieldLabel": "Test3",
          "hasMutlipleValues": false,
          "multiValues": [],
          "fieldValue": "Test4"
        }
      ]
    }
  ],
  "total": 1
}

I think the best way would be to store the json itself in db.我认为最好的方法是将 json 本身存储在数据库中。 Since you mentioned the JSON is dynamic in nature.由于您提到 JSON 本质上是动态的。

This is with mariadb.这是 mariadb。

CREATE TABLE Sample (id int, json_file JSON, PRIMARY KEY(id));

If the above method doesn't work you can convert the json to string and then store in db in a VARCHAR field.如果上述方法不起作用,您可以将 json 转换为字符串,然后存储在 db 中的 VARCHAR 字段中。

import json
json_string = json.dump(json_file)

Creating columns as they are generated is something I feel is unnecessary.在生成列时创建列是我觉得没有必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python类中存储嵌套数据的最佳方法是什么? - What is the best way to store nested data in a python class? 如何使用python将从api网址下载的json数据解析和存储到SQLite DB表中 - How to parse and store json data downloaded from api url into SQLite DB table using python 在 AWS 中存储从第三方 API 生成的访问令牌的最佳方式是什么 - What's the best way to store access token generated from third party API in AWS 在Python中存储设置数据的最佳方法是什么? - What is the best way to store set data in Python? Python 和 Json - 从 API 中提取数据部分的最佳方法? - Python and Json - Best way to pull out sections of data from API? 使用 Python 将 JSON 数据从 API 发送到 Firestore 的最佳方式 - Best way to send JSON data from API to Firestore with Python 使用 Python 将数据从 REST API 导入 SQL DB 的更好方法? - Better way to import data from REST API to SQL DB using Python? 将Python生成的数据发送到PHP的最佳方法是什么? - What is the best way to send Python generated data to PHP? 从 api 响应中解析嵌套 json 的最佳方法 - Best way to parse nested json from an api response 使用Ruby或Python存储大量外部API数据的最佳方法 - Best Way to Store Large Amount of Outside API Data… using Ruby or Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM