在查询中返回未知 JSON
Returning unknown JSON in a query
这是我的场景。我在 Cosmos DB 中有数据,我想 return c.this、c.that 等作为 Azure 认知搜索的索引器。我想要 return 的一个字段是未知结构的 JSON。我唯一知道的是它是平的。但是,据我了解,需要知道索引器的 return 值。如何在 SELECT 中使用 SQL,我会 return 平面对象中的所有 JSON 元素?这是我要查询的示例值:
</p>
<pre><code>{
"BusinessKey": "SomeKey",
"Source": "flat",
"id": "SomeId",
"attributes": {
"Source": "flat",
"Element": "element",
"SomeOtherElement": "someOtherElement"
}
}
所以我希望我的 select 可能是这样的:
</p>
<pre><code>SELECT
c.BusinessKey,
c.Source,
c.id,
-- SOMETHING HERE TO LIST OUT ALL ATTRIBUTES IN THE JSON AS FIELDS IN THE RESULT
我希望结果是:
</p>
<pre><code>{
"BusinessKey": "SomeKey",
"Source": "flat",
"id": "SomeId",
"attributes": [{"Source":"flat"},{"Element":"element"},{"SomeOtherElement":"someotherelement"}]
}
目前我们在 c.attributes 上调用 ToString,它是未知结构的 JSON 但它正在添加所有转义字符。当我们要搜索索引时,我们必须添加所有这些转义字符,这真的很不守规矩。
有没有办法使用 SQL 来做到这一点?
感谢您的帮助!
您可以在 cosmos db sql 中使用 UDF。
UDF代码:
function userDefinedFunction(object){
var returnArray = [];
for (var key in object) {
var map = {};
map[key] = object[key];
returnArray.push(map);
}
return returnArray;
}
Sql:
SELECT
c.BusinessKey,
c.Source,
c.id,
udf.test(c.attributes) as attributes
from c
输出:
这是我的场景。我在 Cosmos DB 中有数据,我想 return c.this、c.that 等作为 Azure 认知搜索的索引器。我想要 return 的一个字段是未知结构的 JSON。我唯一知道的是它是平的。但是,据我了解,需要知道索引器的 return 值。如何在 SELECT 中使用 SQL,我会 return 平面对象中的所有 JSON 元素?这是我要查询的示例值:
</p>
<pre><code>{
"BusinessKey": "SomeKey",
"Source": "flat",
"id": "SomeId",
"attributes": {
"Source": "flat",
"Element": "element",
"SomeOtherElement": "someOtherElement"
}
}
所以我希望我的 select 可能是这样的:
</p>
<pre><code>SELECT
c.BusinessKey,
c.Source,
c.id,
-- SOMETHING HERE TO LIST OUT ALL ATTRIBUTES IN THE JSON AS FIELDS IN THE RESULT
我希望结果是:
</p>
<pre><code>{
"BusinessKey": "SomeKey",
"Source": "flat",
"id": "SomeId",
"attributes": [{"Source":"flat"},{"Element":"element"},{"SomeOtherElement":"someotherelement"}]
}
目前我们在 c.attributes 上调用 ToString,它是未知结构的 JSON 但它正在添加所有转义字符。当我们要搜索索引时,我们必须添加所有这些转义字符,这真的很不守规矩。
有没有办法使用 SQL 来做到这一点?
感谢您的帮助!
您可以在 cosmos db sql 中使用 UDF。
UDF代码:
function userDefinedFunction(object){
var returnArray = [];
for (var key in object) {
var map = {};
map[key] = object[key];
returnArray.push(map);
}
return returnArray;
}
Sql:
SELECT
c.BusinessKey,
c.Source,
c.id,
udf.test(c.attributes) as attributes
from c
输出: