对于 JSON 路径格式
For JSON Path Formatting
背景:
我有一个名为 'questions' 的 JSON nvarchar(max)
列,它看起来像单行中的真实示例...
{"211":0,"212":0,"213":0,"214":0,"215":0,"216":0,"217":0,"218":0,"219":0,"220":"1","221":"1","222":"1","223":"1","224":"1","225":"1","226":"1","227":"1","228":"1","229":"1","230":"1","231":"1","232":"1"}
我目前正在生成此示例 JSON 片段作为示例 'call'...
[
{
"call": {
"id": 200643,
"yes_answers": [
{
"question_id": "220"
},
{
"question_id": "221"
},
{
"question_id": "222"
},
{
"question_id": "223"
},
{
"question_id": "224"
},
{
"question_id": "225"
},
{
"question_id": "226"
},
{
"question_id": "227"
},
{
"question_id": "228"
},
{
"question_id": "229"
},
{
"question_id": "230"
},
{
"question_id": "231"
},
{
"question_id": "232"
}
]
}
}
]
.. 使用此查询...
select c.call_id as [call.id],
(
select x.[key]
from [call].[triage_questions] tq
cross apply openjson(questions, '$') as x
where value = 1 and tq.call_id = c.call_id
for json path
) as [call.yes_answers]
from [call].[dimension] c
where call_id = 200643
for json path
我的问题:
我不喜欢 "yes_answers" 数组的格式设置方式。我想要更多这样的东西:
[
{
"call": {
"id": 200643,
"yes_answers": [
220,
221,
222,
223,
224,
225,
226,
227,
228,
229,
230,
231,
232
]
}
}
]
这有效吗?还是我应该以不同的方式进行滚动?还是留下?
我将使用 SQL 将 "yes_answers" 数组中的每个 question_id 引用到我正在寻找 [=56= 的引用 table ] 与该问题相关的标志。如果重要的话,JSON 目前不会离开 SQL。
编辑:
多亏了 @Tomato32, I've found ,我觉得这让我离得足够近了。
我的查询现在看起来像这样...
select c.call_id as [call.id],
json_query(replace(replace((
select x.[key] as question_id
from [call].[triage_questions] tq
cross apply openjson(questions, '$') as x
where value = 1 and tq.call_id = c.call_id
order by x.[key]
for json path
), N'{"question_id":', N''), '"}', '"')) as [call.yes_answers]
from [call].[dimension] c
where call_id = 200643
for json path
我的结果 JSON 看起来像这样...
[
{
"call": {
"id": 200643,
"yes_answers": [
"220",
"221",
"222",
"223",
"224",
"225",
"226",
"227",
"228",
"229",
"230",
"231",
"232"
]
}
}
]
我无法去掉 "yes_answers" 数组中 int 值周围的引号,但我认为这无关紧要,我不会在这上面浪费任何时间:) 谢谢大家!!!
我只更新了您的替换语句以删除“[number]”中的引号,如下所示:-
select c.call_id as [call.id],
json_query(replace(replace((
select x.[key] as question_id
from [call].[triage_questions] tq
cross apply openjson(questions, '$') as x
where value = 1 and tq.call_id = c.call_id
order by x.[key]
for json path
), N'{"question_id":"', N''), '"}', '')) as [call.yes_answers]
from [call].[dimension] c
where call_id = 200643
for json path
在我的测试中,输出如下:-
[{"call":{"id":200643,"yes_answers":[220,221,222,223,224,225,226,227,228,229,230,231,232]}}]
背景:
我有一个名为 'questions' 的 JSON nvarchar(max)
列,它看起来像单行中的真实示例...
{"211":0,"212":0,"213":0,"214":0,"215":0,"216":0,"217":0,"218":0,"219":0,"220":"1","221":"1","222":"1","223":"1","224":"1","225":"1","226":"1","227":"1","228":"1","229":"1","230":"1","231":"1","232":"1"}
我目前正在生成此示例 JSON 片段作为示例 'call'...
[
{
"call": {
"id": 200643,
"yes_answers": [
{
"question_id": "220"
},
{
"question_id": "221"
},
{
"question_id": "222"
},
{
"question_id": "223"
},
{
"question_id": "224"
},
{
"question_id": "225"
},
{
"question_id": "226"
},
{
"question_id": "227"
},
{
"question_id": "228"
},
{
"question_id": "229"
},
{
"question_id": "230"
},
{
"question_id": "231"
},
{
"question_id": "232"
}
]
}
}
]
.. 使用此查询...
select c.call_id as [call.id],
(
select x.[key]
from [call].[triage_questions] tq
cross apply openjson(questions, '$') as x
where value = 1 and tq.call_id = c.call_id
for json path
) as [call.yes_answers]
from [call].[dimension] c
where call_id = 200643
for json path
我的问题:
我不喜欢 "yes_answers" 数组的格式设置方式。我想要更多这样的东西:
[
{
"call": {
"id": 200643,
"yes_answers": [
220,
221,
222,
223,
224,
225,
226,
227,
228,
229,
230,
231,
232
]
}
}
]
这有效吗?还是我应该以不同的方式进行滚动?还是留下?
我将使用 SQL 将 "yes_answers" 数组中的每个 question_id 引用到我正在寻找 [=56= 的引用 table ] 与该问题相关的标志。如果重要的话,JSON 目前不会离开 SQL。
编辑:
多亏了 @Tomato32, I've found
我的查询现在看起来像这样...
select c.call_id as [call.id],
json_query(replace(replace((
select x.[key] as question_id
from [call].[triage_questions] tq
cross apply openjson(questions, '$') as x
where value = 1 and tq.call_id = c.call_id
order by x.[key]
for json path
), N'{"question_id":', N''), '"}', '"')) as [call.yes_answers]
from [call].[dimension] c
where call_id = 200643
for json path
我的结果 JSON 看起来像这样...
[
{
"call": {
"id": 200643,
"yes_answers": [
"220",
"221",
"222",
"223",
"224",
"225",
"226",
"227",
"228",
"229",
"230",
"231",
"232"
]
}
}
]
我无法去掉 "yes_answers" 数组中 int 值周围的引号,但我认为这无关紧要,我不会在这上面浪费任何时间:) 谢谢大家!!!
我只更新了您的替换语句以删除“[number]”中的引号,如下所示:-
select c.call_id as [call.id],
json_query(replace(replace((
select x.[key] as question_id
from [call].[triage_questions] tq
cross apply openjson(questions, '$') as x
where value = 1 and tq.call_id = c.call_id
order by x.[key]
for json path
), N'{"question_id":"', N''), '"}', '')) as [call.yes_answers]
from [call].[dimension] c
where call_id = 200643
for json path
在我的测试中,输出如下:-
[{"call":{"id":200643,"yes_answers":[220,221,222,223,224,225,226,227,228,229,230,231,232]}}]