I have a json which looks like:
[
{
"TimePeriod": "12/12/23 - 12/26/23",
"ResourceName": "rob brien",
"TimesheetStatus": "Submitted",
"SubmittedBy": "rob brien",
"LastModified": "12/12/23 7:12 AM",
"InvestmentTasks": [
{
"InvestmentID": "PRO13796",
"Investment": "Credit Risk Regulatory ",
"Description": "A3-Dev/Build",
"Hours": {
"12/12": 9,
"12/13": 9,
"12/14": 9,
"12/15": 9,
"12/16": 9,
"12/17": 0,
"12/18": 9,
"12/19": 9,
"12/20": 9,
"12/21": 0,
"12/22": 9,
"12/23": 9,
"12/24": 9,
"12/25": 9,
"12/26": 9,
"Total": 99
}
}
]
}
]
Now, I’m manually mapping the headers in excel in "add a row into table" step (in Power Automate).
Now the issue arrives with dates which are under "Hours" object.
Dates may change either it can increase or decrease or it can be from a different month like 11/1,11/2,11/26 and can get any no of days from a month.
How can I dynamically get these date column headers?
I have come across an article using xpath function:https://mytrial365.com/2023/07/13/discovering-json-value-using-a-dynamic-key-in-power-automate/#:~:text=Discovering%20JSON%20Value%20Using%20a%20Dynamic%20Key%20in%20Power%20Automate,-Kailash%20Ramachandran%20Power&text=Power%20Automate%20offers%20several%20options,outputs%20for%20the%20parsed%20properties.
Xpath Step :xpath(xml(body(‘Parse_JSON_Hours’)), ‘/Hours/*’)
APPLY TO EACH 2 STEP: passing the output of xpath
EACH ITEM: xpath(item(), ‘name(/*)’)
Error: Unable to process template language expressions in action ‘xpath’ inputs at line ‘0’ and column ‘0’: ‘The template language function ‘xml’ parameter is not valid. The provided value cannot be converted to XML: ‘JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName. Path ‘outputs.body.Sat_12_2
I need to get dates header dynamically as no of days are not fixed or it can be for a different month .
Expected output:
12/12 12/13
9 9
so on...
Let me know what I’m doing wrong
2
Answers
I think the easiest way is for you to get what you need is to use the Advanced Data Operations connector and the Json Properties to Name Value Pair Array operation.
https://statesolutions.com.au/json-properties-to-name-value-pair-array/
It will produce this output which you can then loop through and do what you need with …
… the only caveat is that it takes an object, not an array so you can do it for the first row in your dataset and go from there.
For your data set, using the XML approach may be a little troublesome, if I convert your JSON to XML, I get this …
… and as you can see, the slashes don’t really translate too well.
To answer your question though, when converting to XML, you’ll need to use this expression …
… obviously replacing the
variables('Data')
part with your own JSON object.You were close. An XML document needs a root element. And your
Parse JSON Hours
is returning:Which doesn’t have one root property.
Update your
xpath
step to:And that should resolve the error.