Cosmos 嵌套 JSON 查询
Cosmos Nested JSON Query
这是我第一次使用 CosmosDB,我正在尝试创建一个查询来获取有关此问题的一些详细信息JSON:
{
"Status": "Uploaded",
"ProvidedOn": "2022-04-04T18:34:57.4160484Z",
"DocumentTaxonomy": {
"JurisdictionalCountriesOfService": [
{
"Id": 5,
"Name": "United States"
}
],
"WorkProduct": {
"Id": 762,
"Name": "Other reports and documents",
"Type": "Info item"
}
},
"id": "3a92c052-bc23-4b8a-acbf-54044785968a",
"Meta": {
"VersionId": "3",
"LastUpdated": "0001-01-01T00:00:00",
"Source": null,
"Security": null,
"Tag": null,
"Id": null,
"Extension": null,
"ModifierExtension": null
},
}
基本上我需要得到类似
的东西
SELECT id,Status,ProvidedOn, WorkProductName, WorkProductType,MetaVersionId FROM JSONFILE
在这张图片中,我突出显示了我的查询需要的列
注意:因为我需要查询不同的 CosmoDB,并不是所有的 CosmoDB 都有 DocumentTaxonomy 部分,所以计划是在它们不存在时 return 比如 null 或空白值
根据您的问题,代码应该 return DocumentTaxonomy 部分值,如果它们存在于 JSON 中,否则它应该 return 空值或空白值。
此代码可能适合您:
SELECT c.id, c.ProvidedOn, c.Status,c.Meta.VersionId as versionId,
IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Type) = true ? c.DocumentTaxonomy.WorkProduct.Type
: IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Type) = false ? null
: "some default value"
as TypeDoc,
IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Name) = true ? c.DocumentTaxonomy.WorkProduct.Name
: IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Name) = false ? null
: "some default value"
as NameDoc
FROM c
当 DocumentTaxonomy 部分存在时它给出的输出是:
[
{
"id": "3a92c052-bc23-4b8a-acbf-54044785968a",
"ProvidedOn": "2022-04-04T18:34:57.4160484Z",
"Status": "Uploaded",
"versionId": "3",
"TypeDoc": "Info item",
"NameDoc": "Other reports and documents"
}
]
DocumentTaxonomy 部分不存在时的输出:
[
{
"id": "3a92c052-bc23-4b8a-acbf-54044785968a",
"ProvidedOn": "2022-04-04T18:34:57.4160484Z",
"Status": "Uploaded",
"versionId": "3",
"TypeDoc": null,
"NameDoc": null
}
]
请查看输出的屏幕截图以供参考:
这是我第一次使用 CosmosDB,我正在尝试创建一个查询来获取有关此问题的一些详细信息JSON:
{
"Status": "Uploaded",
"ProvidedOn": "2022-04-04T18:34:57.4160484Z",
"DocumentTaxonomy": {
"JurisdictionalCountriesOfService": [
{
"Id": 5,
"Name": "United States"
}
],
"WorkProduct": {
"Id": 762,
"Name": "Other reports and documents",
"Type": "Info item"
}
},
"id": "3a92c052-bc23-4b8a-acbf-54044785968a",
"Meta": {
"VersionId": "3",
"LastUpdated": "0001-01-01T00:00:00",
"Source": null,
"Security": null,
"Tag": null,
"Id": null,
"Extension": null,
"ModifierExtension": null
},
}
基本上我需要得到类似
的东西SELECT id,Status,ProvidedOn, WorkProductName, WorkProductType,MetaVersionId FROM JSONFILE
在这张图片中,我突出显示了我的查询需要的列
注意:因为我需要查询不同的 CosmoDB,并不是所有的 CosmoDB 都有 DocumentTaxonomy 部分,所以计划是在它们不存在时 return 比如 null 或空白值
根据您的问题,代码应该 return DocumentTaxonomy 部分值,如果它们存在于 JSON 中,否则它应该 return 空值或空白值。
此代码可能适合您:
SELECT c.id, c.ProvidedOn, c.Status,c.Meta.VersionId as versionId,
IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Type) = true ? c.DocumentTaxonomy.WorkProduct.Type
: IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Type) = false ? null
: "some default value"
as TypeDoc,
IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Name) = true ? c.DocumentTaxonomy.WorkProduct.Name
: IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Name) = false ? null
: "some default value"
as NameDoc
FROM c
当 DocumentTaxonomy 部分存在时它给出的输出是:
[
{
"id": "3a92c052-bc23-4b8a-acbf-54044785968a",
"ProvidedOn": "2022-04-04T18:34:57.4160484Z",
"Status": "Uploaded",
"versionId": "3",
"TypeDoc": "Info item",
"NameDoc": "Other reports and documents"
}
]
DocumentTaxonomy 部分不存在时的输出:
[
{
"id": "3a92c052-bc23-4b8a-acbf-54044785968a",
"ProvidedOn": "2022-04-04T18:34:57.4160484Z",
"Status": "Uploaded",
"versionId": "3",
"TypeDoc": null,
"NameDoc": null
}
]
请查看输出的屏幕截图以供参考: