I’m trying to get a list of reviews from an api.
This gives me a JSON with nested properties.
I manage to get almost all the data I need except from the nested array ["value"]["answers"]["value"] because sometimes the array ["answers"] has one item (with "id" being either "goods", "delivery" or "service"), or 2 or all 3 of them.
So I can’t use the index to get what I want.
I need to be able to check the "id" and from that append the "value" to my array.
Is there a way to write an expression that checks the "id" and from then on writes the "value" in my array ?
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"
}
]
...
My array so far :
"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']"
}
Thank you
2
Answers
There are a couple of ways to skin this cat, some more efficient than others.
Personally, I would take the following approach.
I created a flow where, basically, the first thing it does is initialise a variable that contains your
Answers
array as the data …The next step is to initialize another variable that stores that array as XML …
This is the expression contained within …
In the next step, I’m basically doing what you’re doing by constructing an object, only this time, the selection for the values are a little more complex, this is the definition of the body …
Using the
xpath
approach for querying the dataset can cut down the amount of steps you need to take to get the desired result.If an element is missing, it will simply come back as null …
Below is one of the workarounds to achieve your requirement. Firstly, I have initialised 3 variables for Delivery, Goods and Service Counts.
Then used
Parse_JSON
to extract the inner values of the JSON provided.In the next step I’m trying to iterate through Id’s using
Switch
action withbody('Parse_JSON')?['value']?['answers']?[iterationIndexes('Until')]?['id']
expression and set the variables using below expression inside an until loop.and finally you can add in your array like below
RESULTS:
You can use below code view to reproduce in your logic app