如何解决从SQL Server 2019数据库表中的json字符串中提取多个值/级别
如何在SQL Server 2019中实现此目标?我有一个带有#Bad code,but simplest in this circumstance.
def iterate(arr):
global queue
for comment in arr:
queue.append(comment["detail"])
print(comment["detail"]) #For your debug.
iterate(comment["responses"])
post = {} #Put your data here
queue = []
iterate(post["comments"])
列的订单表,其中包含一个json字符串,其中包含所运送物品的行项目详细信息。我需要将每个变量ID和数量提取到单独的行中(每个变量ID 1行)。
以下是实际json字符串的示例:
lineaggregate
解决方法
这不是有效的JSON,但是如果我们进行安排以使其有效,那么我们将获得以下内容:
{
"Products": [
{
"id": 4379711799354,"variant_id": 31291380727866,"title": "*NEW* Kayslin Sneaker in Blush","quantity": 1,"sku": "79000212","variant_title": "3Y","vendor": "TESTVENDOR","fulfillment_service": "manual","product_id": 4371426607162,"requires_shipping": true,"taxable": true,"gift_card": false,"name": "*NEW* Kayslin Sneaker in Blush - 3Y","variant_inventory_management": "shopify","properties": [
{
"name": "Item Ships Separately","value": "✓"
}
]
},{
"id": 4379711832122,"variant_id": 31031946838074,"title": "*NEW* Mama Bird Necklace Set in Gold","sku": "83000109","variant_title": "","product_id": 4320751878202,"name": "*NEW* Mama Bird Necklace Set in Gold","properties": [],"product_exists": true,"fulfillable_quantity": 0,"grams": 113,"price": "29.50","total_discount": "0.00","fulfillment_status": "fulfilled","pre_tax_price": "29.50","price_set": {
"shop_money": {
"amount": "29.50","currency_code": "USD"
},"presentment_money": {
"amount": "29.50","currency_code": "USD"
}
},"pre_tax_price_set": {
"shop_money": {
"amount": "29.50","total_discount_set": {
"shop_money": {
"amount": "0.00","presentment_money": {
"amount": "0.00","currency_code": "USD"
}
} } ] }
然后,您可以使用OPENJSON()
函数并在查询中加入CROSS APPLY
连接来提取元素:
SELECT Q2.*
FROM tab
CROSS APPLY OPENJSON(JsonData,'$.Products')
WITH (
Products nvarchar(max) '$' AS JSON
) Q1
CROSS APPLY OPENJSON (Q1.Products)
WITH (
ID nvarchar(max) '$.id',variant_id nvarchar(max) '$.variant_id',quantity nvarchar(max) '$.quantity',properties nvarchar(max) '$.properties[0].name',properties nvarchar(max) '$.properties[0].value',price_shop_money_amt nvarchar(max) '$.price_set.shop_money.amount',price_shop_money_cur nvarchar(max) '$.price_set.shop_money.currency_code'
) Q2
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。