gpt4 book ai didi

arrays - Azure逻辑应用程序根据值访问json属性

转载 作者:行者123 更新时间:2023-12-03 03:38:25 24 4
gpt4 key购买 nike

我正在尝试从 api 获取评论列表。这给了我一个带有嵌套属性的 JSON。我设法获取几乎所有我需要的数据,除了嵌套数组 [“value”][“answers”][“value”] 之外,因为有时数组 [“answers”] 有一项(“id”为“ cargo ”、“交付”或“服务”),或其中 2 项或全部 3 项。所以我无法使用索引来获得我想要的东西。我需要能够检查“id”并将“值”附加到我的数组中。有没有办法编写一个表达式来检查“id”,然后将“值”写入我的数组中?

JSON:

...
"surveyData": [
{
"questionRef": "q1",
"type": "CORE_STAR_RATING",
"properties": {
"title": "How do you evaluate ...?"
},
"value": {
"stars": 5,
"notAnswered": false
},
"userDefinedId": "q1"
},

"userDefinedId": "q2-good"
},
{
"questionRef": "q3",
"type": "DIMENSIONS",
"properties": {
"title": "How do you evaluate on those criterias ?"
},
**"value": {
"answers": [
{
"id": "delivery",
"value": 4,
"userDefinedId": "delivery",
"name": "Delivery",
"firstLabel": "Nul",
"lastLabel": "Good"
},
{
"id": "goods",
"value": 5,
"userDefinedId": "goods",
"name": "Goods",
"firstLabel": "Nul",
"lastLabel": "Good"
},
{
"id": "service",
"value": 5,
"userDefinedId": "service",
"name": "Service",
"firstLabel": "Nul",
"lastLabel": "Good"
}
]**
},
"userDefinedId": "q3-group"
}
]
...

到目前为止我的数组:


"value": {
"Event": "@items('For_each-Service')?['event']?['type']",
"Delivery": "", <= ??
"Goods": "", <= ??
"Service": "@items('For_each-Service')?['surveyData'][2]?['value']?['answers'][2]?['value']", <= doesn't work
"comment": "@items('For_each-Service')?['reply']?['comment']",
"order date": "@items('For_each-Service')?['transaction']?['date']",
"date": "@items('For_each-Service')?['reply']?['createdAt']",
"email": "@items('For_each-Service')?['customer']?['email']",
"id review": "@items('For_each-Service')?['event']?['id']",
"name client": "@items('For_each-Service')?['customer']?['fullName']",
"status": "@items('For_each-Service')?['state']",
"title": "@items('For_each-Service')?['title']"
}

谢谢

最佳答案

有几种方法可以给这只猫剥皮,其中一些方法比其他方法更有效。

就我个人而言,我会采取以下方法。

我创建了一个流程,基本上,它所做的第一件事就是初始化一个变量,其中包含您的 Answers 数组作为数据...

Answers

下一步是初始化另一个变量,将该数组存储为 XML ...

XML

这是...中包含的表达式

xml(json(concat('{ "root": { "answer": ', variables('Answers Array'), '}}')))

在下一步中,我基本上通过构造一个对象来完成您正在做的事情,只是这一次,值的选择有点复杂,这是主体的定义......

Object

{
"Delivery": @{first(xpath(xml(variables('XML')), '//id[contains(text(), "delivery")]/parent::answer/value/text()'))},
"Goods": @{first(xpath(xml(variables('XML')), '//id[contains(text(), "goods")]/parent::answer/value/text()'))},
"Service": @{first(xpath(xml(variables('XML')), '//id[contains(text(), "service")]/parent::answer/value/text()'))}
}

使用 xpath 方法查询数据集可以减少获得所需结果所需的步骤。

如果某个元素丢失,它将简单地返回为 null ...

Result

关于arrays - Azure逻辑应用程序根据值访问json属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72432289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com