我使用的是SQL Server,其中有一列将json存储为字符串.我正在try 将json分解/规范化为新的行和列.下面是表格当前外观的图片.每个PricePointID在价格json列中至少有一条记录.每个JSON数组在每一行中都有相同的键.将 for each 价格对象创建新的行,然后每个键将成为其自己的列.

View of the table.

一个PricePointID的例子,它在价格json数组中有多个价格对象,应该 for each 价格id分成多行.

# PricePointId
40844
# Prices
[{"id":252820,"component_id":106965,"starting_quantity":1,"ending_quantity":1,"unit_price":"20.0","price_point_id":40844,"formatted_unit_price":"$20.00","segment_id":null},{"id":595550,"component_id":106965,"starting_quantity":2,"ending_quantity":5,"unit_price":"10.0","price_point_id":40844,"formatted_unit_price":"$10.00","segment_id":null},{"id":595551,"component_id":106965,"starting_quantity":6,"ending_quantity":null,"unit_price":"5.0","price_point_id":40844,"formatted_unit_price":"$5.00","segment_id":null}]

Desired results should look like this mock up in excel. Each object for the associated PricePointId has been given it's own row and the keys have been turned into columns.
Desired Results

我try 了OPENJSON和STRING_SPLIT,但似乎两者都不起作用.

推荐答案

我只是想对我的 comments 做进一步的说明

Select A.[PricePointId] 
      ,B.*
 From  YourTable A
 Cross Apply ( Select *
                 From OpenJSON(Prices)
                 with ( component_id         int
                       ,starting_quantity    int
                       ,ending_quantity      int
                       ,unit_price           money
                       ,price_point_id       int
                       ,formatted_unit_price varchar(50)
                       ,segment_id           varchar(50)
                      )
             ) B

Results

enter image description here

Json相关问答推荐

如何在PowerShell中扩展JSON中的嵌套数组

JSON:将项';S键/名称移动到属性中,并使用JQ将其转换为数组

Vega图表计数聚合如果数据值为空数组则不显示任何内容,如何解决此问题?

在 SQL 存储过程中使用参数 Select 值

Jolt Spec 跳过数组中的第一个元素

从 oracle 数据库中的 json blob 打印值

修改 boost::json::object 中的值

在 PostgreSQL 中 Select 分层 JSON 作为表

JQuery,使用 GET 方法发送 JSON 对象

如何判断 Json 对象中是否存在键并获取其值

JSON Schema 与 XML Schema 的比较及其future

JSON 模式验证

Jackson Json:如何将数组转换为 JsonNode 和 ObjectNode?

什么是类型和类型令牌?

如何将有向无环图 (DAG) 存储为 JSON?

与classic 规范化表相比,postgres JSON 索引是否足够高效?

如何让 javascript 从 .json 文件中读取?

将 json 转换为 C# 数组?

添加json文件注释

用 JSON 编写 HTML 字符串