web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Xpath addProperty want...
Power Automate
Unanswered

Xpath addProperty want all the parent properties too...

(0) ShareShare
ReportReport
Posted on by 678

I'm using a select action and it's return what I'd describe as the meta data about the rows it found...

addProperty(item(),'Comments',xpath(outputs('XMLComments'),concat('//Array[ProjectId/text()="', item()['ID'],'"]')))

see image...
Region Capture.jpg
How do I get all the object properties for these comments? I've tried different things and the closest I've got so far is...

addProperty(item(),'Comments',xpath(outputs('XMLComments'),concat('//Array[ProjectId/text()="', item()['ID'],'"]/*/node()')))

 Comments should contain an object containing... ID, Title, ProjectId, Modified, Created, etc.. It is correct that it is recognizing 3 objects in the Comments array so I know I'm close

Categories:
I have the same question (0)
  • sasrsc Profile Picture
    678 on at

    I'm trying everything under the sun.... for example if I do...

    addProperty(item(),'Comments',xpath(outputs('XMLComments'),concat('//Array[ProjectId/text()="', item()['ID'],'"]/Title/text()')))

    I get this...again it's pulling what it is told and the fact there's an array of 3 for the first project is correct and zero for the 2nd is spot on.

    Region Capture.jpg

    Here's a snippet of the XMLComments that it is searching through to find the appropriate comments. The xpath entry is finding everything with the ProjectId node value of [whatever]. Then when it does it needs to display all the siblings and the node values in the new Comments array. Not just the value of the title as immediately above. The screen shot below shows the whole "XMLComments" object within a massive array. Even though it's a different ProjectID to above the object is the same. I hard code the title value to "Comments" as below and as reflected above.
    commentsarray.jpg

    My goal is append an array .. where the ProjectId matches in the example below 74

    "Comments": [ {the whole XMLComments object} that match zero or more}]

    so 

    "Comments": [
    {
       "ItemInternalId": "299",
        "ID": 299,

        "ProjectId": 74
        "Title": "Comments",
        "Comment": "Product Blitz blah,...."

         other properties...
    },

    {
       "ItemInternalId": "456",
        "ID": 456,

         "ProjectId": 74
        "Title": "Comments",
        "Comment": "some other boring comment...."

        other properties...
    },
    .... more objects if they match on the ProjectId...

    ]

  • grantjenkins Profile Picture
    11,063 Moderator on at

    @sasrsc A couple of questions.

    1. Are you able to show the raw complex JSON data you are using to build up your final output?
    2. Do you have two sets of JSON that you are trying to join together?
  • sasrsc Profile Picture
    678 on at

    So I believe that the original $content returned at the top of this ticket is base64 and if I can convert that in the select "addProperty" function then it may work. That's the angle I'm going now trying to wrap that original entry with a base64ToString but it's an array so it doesn't like it. 
    The rabbit hole I went down came from...
    https://sharepains.com/2018/04/27/microsoft-flow-create-sharepoint-list-items-using-xml/ and https://powerusers.microsoft.com/t5/Building-Flows/Using-XPATH-over-XML-file-return-coded-anwer/td-p/572556 
    To answer your question - yes, I am joining two sets of Json together using Xpath (as it's so much quicker than looping). I convert the SP lists to an array, then to xml following the syntax inspired by @Paul Muranain this post https://www.tachytelic.net/2022/07/power-automate-merge-array/

    To final json object in the end is like the top example except the Comments property would contain an array of comments containing the actual values (there are numerous) of the matched comments but not in base64!



  • sasrsc Profile Picture
    678 on at

    Fascinating rabbit hole this... one thing leads to another. So I'm correct that the original string value for $content is base64 because if I do a compose on the value and then convert that from base64ToString I see what I really want in the output...

    base64ToString(first(body('Select_2'))?['Comments'][0]?['$content'])

    base64.jpg
    So the real question then is ... in the original AddProperty statement I really need to ...
    1) How do I pull only the $content

    2) How do I convert that from base64 

    Here's the original line...

    addProperty(item(),'Comments',xpath(outputs('XMLComments'),concat('//Array[ProjectId/text()="', item()['ID'],'"]')))




  • grantjenkins Profile Picture
    11,063 Moderator on at

    I'll try to get a solution for you tomorrow - just off to sleep now (1AM for me).

  • Verified answer
    grantjenkins Profile Picture
    11,063 Moderator on at

    I've been trying to get this using XPath but struggling with the same as you - converting each of the values returned from the XPath expression to JSON. I'm not even sure if it's possible given we don't have an each we can use within the items returned.

     

    Just a quick question - how many items are you working with? If not a massive amount, then you could still do the following using just JSON which would be fairly efficient.

     

    See full flow below. I'll go into each of the actions.

    grantjenkins_0-1671327883544.png

     

    For this example, I've used the following data for the items.

    [
     {
     "id": "1",
     "title": "Item 001"
     },
     {
     "id": "2",
     "title": "Item 002"
     },
     {
     "id": "3",
     "title": "Item 003"
     }
    ]

    grantjenkins_1-1671327937668.png

     

    And for the comments:

    {
     "comments": [
     {
     "id": "1",
     "projectId": "1",
     "comment": "This is the first comment for Item 001"
     },
     {
     "id": "2",
     "projectId": "1",
     "comment": "This is the second comment for Item 001"
     },
     {
     "id": "3",
     "projectId": "2",
     "comment": "This is the first comment for Item 002"
     },
     {
     "id": "4",
     "projectId": "3",
     "comment": "This is the first comment for Item 003"
     }
     ]
    }

    grantjenkins_2-1671328013857.png

     

    Apply to each iterates over each of the items in the Items.

    grantjenkins_3-1671328101599.png

     

    Filter array takes in the items in the Comments array. This will return all the comments associated with the current item.

    outputs('Comments')?['comments']

     

    And the Condition:

    item()?['projectId']
    
    items('Apply_to_each')?['id']

    grantjenkins_4-1671328228385.png

     

    Compose will add a new property using the following expression:

    addProperty(items('Apply_to_each'), 'comments', body('Filter_array'))

    grantjenkins_5-1671328318632.png

     

    Data will then take the output from the Compose that should contain all the data you want.

    grantjenkins_6-1671328415916.png

     

    The final output from Data would look like that below.

    [
     {
     "id": "1",
     "title": "Item 001",
     "comments": [
     {
     "id": "1",
     "projectId": "1",
     "comment": "This is the first comment for Item 001"
     },
     {
     "id": "2",
     "projectId": "1",
     "comment": "This is the second comment for Item 001"
     }
     ]
     },
     {
     "id": "2",
     "title": "Item 002",
     "comments": [
     {
     "id": "3",
     "projectId": "2",
     "comment": "This is the first comment for Item 002"
     }
     ]
     },
     {
     "id": "3",
     "title": "Item 003",
     "comments": [
     {
     "id": "4",
     "projectId": "3",
     "comment": "This is the first comment for Item 003"
     }
     ]
     }
    ]

     

    This is fairly efficient but depends on how many projects you are iterating over.

  • sasrsc Profile Picture
    678 on at

    I have about 100 projects, 700 comments and about 500 milestones (what I'm doing with comments I need to do with milestones too). Each list has about 10 variables but there are some complex people ones which bloat the response of course.

     

    I started with the apply to each before going down the xpath route but got confused how to handle, because on the 1st loop it would retain the value and then on the 2nd iteration it would update the 2nd value and the 1st value comments were erased. I was getting confused there obviously between temp loop values and the true output. But I think I may have added some more stuff to both it up. I'll try your method and see. You've explained it very well.

  • sasrsc Profile Picture
    678 on at

    That worked. I needed to do the same thing with Milestones too so I converted the data output with ParseJson and did another loop with the filter milestones and then addProperty after that and it appears at first glance to have worked. I tried initially to put in the same apply to each but I couldn't get it to take Comments and Milestones. Not sure what I was doing wrong. The run time wasn't bad in the test that I ran as it filters the array quickly. 

  • Paulie78 Profile Picture
    8,422 Moderator on at

    You almost had it right with your xpath, but you were selecting the entire object and not the specific element that you wanted to capture. On top of that you were not converting the output to text in the xpath. Here is the sample flow that I made:

    2022-12-18_21-57-07.png

     

    The expression in the select is:

    addProperty(item(), 'Comments', xpath(outputs('XMLComments'),concat('//Comments[ProjectId/text()="', item()['ID'],'"]/Title/text()')))

    The example xpath expression would be:

    //Comments[ProjectId/text()="74"]/Title/text()

    As you can see it first filters down to the ProjectID containing the ID of the current item but then navigates to the sibling property of Title and returns that as an array of text. The result in this basic example is:

    [
     {
     "ID": 74,
     "Comments": [
     "1st Comment",
     "2nd Comment",
     "3rd Comment"
     ]
     }
    ]

     

    Hope this helps

     

    Blog: tachytelic.net

    YouTube: https://www.youtube.com/c/PaulieM/videos

    If I answered your question, please accept it as a solution 😘

  • sasrsc Profile Picture
    678 on at

    Hey @Paulie78 I actually need all of the comments objects, not just the title and I need the property name.
    So I need...

    [
     {
     "ID": 74,
     "Comments" : [
     {"ID": 1, "Title": "1st comment", "ProjectId": 74 },
     {"ID": 2, "Title": "2nd comment", "ProjectId": 74 },
     {"ID": 3, "Title": "3rd comment", "ProjectId": 74 }
     ]
     },
     {
     "ID": 75,
     "Comments" : [
     {"ID": 4, "Title": "1st comment", "ProjectId": 75 }
     ]
     }
    ]
     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard