【问题标题】:Inserting JSON data from python into SQL Server将 JSON 数据从 python 插入 SQL Server
【发布时间】:2022-01-18 23:18:14
【问题描述】:

我通过 Python 脚本调用 API 以检索数据。我使用以下方法将此数据存储到 JSON 文件中:

  with open('response.json()', 'w') as outfile:
   json5.dump(response.json(), outfile, ensure_ascii=False, indent=4) 

请在下面找到 JSON 文件的内容:

 {
query: {
    dimensions: [
        "Products.desc",
        "Products.category",
   ],
    measures: [
        "Products.price",
        "Products.amount",
   ],
    filters: [
        {
            operator: "inDateRange",
            values: [
                "2021-11-12",
            ],
            member: "Products.createdDate",
        },
        {
            operator: "equals",
            values: [
                "Scotland",
            ],
            member: "Products.region",
        },
    ],
    timezone: "UTC",
    order: [],
    timeDimensions: [],
},
data: [
    {
        "Products.desc": "Pens",
        "Products.category": "Stationery",
        "Products.price": "10.15000000",
        "Products.amount": "5",
   },
    {
        "Products.desc": "Bags",
        "Products.category": "Clothing",
        "Products.price": "40.5000000",
        "Products.amount": "10",
    },          
    {
       "Products.desc": "Drink",
       "Products.category": "Foods",
       "Products.price": "15.2000000",
       "Products.amount": "20",
     },
],
lastRefreshTime: "2021-12-15T12:48:10.230Z",
annotation: {
    measures: {
        "Products.price": {
            title: "Products Price",
            shortTitle: "Products Total Price",
            type: "number",
            drillMembers: [],
            drillMembersGrouped: {
                measures: [],
                dimensions: [],
            },
        },
        "Products.amount": {
            title: "Products Amount",
            shortTitle: "Products Total Amount",
            type: "number",
            drillMembers: [],
            drillMembersGrouped: {
                measures: [],
                dimensions: [],
            },
         },
    },
    dimensions: {
        "Products.desc": {
            title: "Products description",
            shortTitle: "Desc",
            type: "string",
        },
        "Products.category": {
            title: "Products category",
            shortTitle: "Category",
            type: "string",
        },
      
     segments: {},
    timeDimensions: {},
},
slowQuery: false,

}

我尝试将值提取到 SQL Server 中的表中,但无济于事。

DECLARE @JSON NVARCHAR(MAX)

SELECT @JSON = BulkColumn
FROM OPENROWSET 
(BULK N'[Folder path]', SINGLE_CLOB) 
AS j

SELECT ISJSON(@JSON) AS IsValidJson;

它没有将其识别为有效的 JSON,并收到错误消息“JSON 文本格式不正确。在位置 7 找到了意外的字符 'q'。”

我只想将数据数组中的值提取到一个表中,如下所示:

enter image description here

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 提问时,您需要提供minimal reproducible example: (1) DDL 和样本数据填充,即 CREATE 表和 INSERT T-SQL 语句。 (2) 你需要做什么,即逻辑和你的代码尝试在 T-SQL 中实现它。 (3) 期望的输出,基于上述#1 中的样本数据。 (4) 您的 SQL Server 版本 (SELECT @@version;)。全部在问题内,没有图片。
  • 请编辑问题,并完整提供有问题的 JSON。它看起来被截断了。
  • 所有 JSON 键都应该用双引号括起来。
  • 对此表示歉意。

标签: python sql arrays json sql-server


【解决方案1】:

我使用以下方法将此数据存储到 JSON 文件中:

不,你没有这样做。您正在将数据存储到 JSON5 文件中。

如果要将数据存储到 JSON 文件中,请使用 json 模块,而不是 json5 模块:

  with open('response.json()', 'w') as outfile:
   json.dump(response.json(), outfile, ensure_ascii=False, indent=4) 

【讨论】:

  • 非常感谢。我正在使用 PyCharm,并且只有 JSON5 包可用。我能够在脚本上使用 JSON。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
相关资源
最近更新 更多