cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
sasrsc
Continued Contributor
Continued Contributor

Xpath addProperty want all the parent properties too...

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

1 ACCEPTED SOLUTION

Accepted Solutions

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.


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.

View solution in original post

10 REPLIES 10
sasrsc
Continued Contributor
Continued Contributor

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...

]

@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?

----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.
sasrsc
Continued Contributor
Continued Contributor

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... 
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
Continued Contributor
Continued Contributor

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'],'"]')))




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


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.

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.


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.
sasrsc
Continued Contributor
Continued Contributor

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
Continued Contributor
Continued Contributor

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
Super User
Super User

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
Continued Contributor
Continued Contributor

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 }
    ]
   }
]
  

 

Helpful resources

Announcements

Celebrating the May Super User of the Month: Laurens Martens

  @LaurensM  is an exceptional contributor to the Power Platform Community. Super Users like Laurens inspire others through their example, encouragement, and active participation. We are excited to celebrated Laurens as our Super User of the Month for May 2024.   Consistent Engagement:  He consistently engages with the community by answering forum questions, sharing insights, and providing solutions. Laurens dedication helps other users find answers and overcome challenges.   Community Expertise: As a Super User, Laurens plays a crucial role in maintaining a knowledge sharing environment. Always ensuring a positive experience for everyone.   Leadership: He shares valuable insights on community growth, engagement, and future trends. Their contributions help shape the Power Platform Community.   Congratulations, Laurens Martens, for your outstanding work! Keep inspiring others and making a difference in the community!   Keep up the fantastic work!        

Check out the Copilot Studio Cookbook today!

We are excited to announce our new Copilot Cookbook Gallery in the Copilot Studio Community. We can't wait for you to share your expertise and your experience!    Join us for an amazing opportunity where you'll be one of the first to contribute to the Copilot Cookbook—your ultimate guide to mastering Microsoft Copilot. Whether you're seeking inspiration or grappling with a challenge while crafting apps, you probably already know that Copilot Cookbook is your reliable assistant, offering a wealth of tips and tricks at your fingertips--and we want you to add your expertise. What can you "cook" up?   Click this link to get started: https://aka.ms/CS_Copilot_Cookbook_Gallery   Don't miss out on this exclusive opportunity to be one of the first in the Community to share your app creation journey with Copilot. We'll be announcing a Cookbook Challenge very soon and want to make sure you one of the first "cooks" in the kitchen.   Don't miss your moment--start submitting in the Copilot Cookbook Gallery today!     Thank you,  Engagement Team

Announcing Power Apps Copilot Cookbook Gallery

We are excited to share that the all-new Copilot Cookbook Gallery for Power Apps is now available in the Power Apps Community, full of tips and tricks on how to best use Microsoft Copilot as you develop and create in Power Apps. The new Copilot Cookbook is your go-to resource when you need inspiration--or when you're stuck--and aren't sure how to best partner with Copilot while creating apps.   Whether you're looking for the best prompts or just want to know about responsible AI use, visit Copilot Cookbook for regular updates you can rely on--while also serving up some of your greatest tips and tricks for the Community. Check Out the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community.  We can't wait to see what you "cook" up!    

Welcome to the Power Automate Community

You are now a part of a fast-growing vibrant group of peers and industry experts who are here to network, share knowledge, and even have a little fun.   Now that you are a member, you can enjoy the following resources:   Welcome to the Community   News & Announcements: The is your place to get all the latest news around community events and announcements. This is where we share with the community what is going on and how to participate.  Be sure to subscribe to this board and not miss an announcement.   Get Help with Power Automate Forums: If you're looking for support with any part of Power Automate, our forums are the place to go. From General Power Automate forums to Using Connectors, Building Flows and Using Flows.  You will find thousands of technical professionals, and Super Users with years of experience who are ready and eager to answer your questions. You now have the ability to post, reply and give "kudos" on the Power Automate community forums. Make sure you conduct a quick search before creating a new post because your question may have already been asked and answered. Galleries: The galleries are full of content and can assist you with information on creating a flow in our Webinars and Video Gallery, and the ability to share the flows you have created in the Power Automate Cookbook.  Stay connected with the Community Connections & How-To Videos from the Microsoft Community Team. Check out the awesome content being shared there today.   Power Automate Community Blog: Over the years, more than 700 Power Automate Community Blog articles have been written and published by our thriving community. Our community members have learned some excellent tips and have keen insights on the future of process automation. In the Power Automate Community Blog, you can read the latest Power Automate-related posts from our community blog authors around the world. Let us know if you'd like to become an author and contribute your own writing — everything Power Automate-related is welcome.   Community Support: Check out and learn more about Using the Community for tips & tricks. Let us know in the Community Feedback  board if you have any questions or comments about your community experience. Again, we are so excited to welcome you to the Microsoft Power Automate community family. Whether you are brand new to the world of process automation or you are a seasoned Power Automate veteran - our goal is to shape the community to be your 'go to' for support, networking, education, inspiration and encouragement as we enjoy this adventure together.     Power Automate Community Team

Hear what's next for the Power Up Program

Hear from Principal Program Manager, Dimpi Gandhi, to discover the latest enhancements to the Microsoft #PowerUpProgram, including a new accelerated video-based curriculum crafted with the expertise of Microsoft MVPs, Rory Neary and Charlie Phipps-Bennett. If you’d like to hear what’s coming next, click the link below to sign up today! https://aka.ms/PowerUp  

Tuesday Tip | How to Report Spam in Our Community

It's time for another TUESDAY TIPS, your weekly connection with the most insightful tips and tricks that empower both newcomers and veterans in the Power Platform Community! Every Tuesday, we bring you a curated selection of the finest advice, distilled from the resources and tools in the Community. Whether you’re a seasoned member or just getting started, Tuesday Tips are the perfect compass guiding you across the dynamic landscape of the Power Platform Community.   As our community family expands each week, we revisit our essential tools, tips, and tricks to ensure you’re well-versed in the community’s pulse. Keep an eye on the News & Announcements for your weekly Tuesday Tips—you never know what you may learn!   Today's Tip: How to Report Spam in Our Community We strive to maintain a professional and helpful community, and part of that effort involves keeping our platform free of spam. If you encounter a post that you believe is spam, please follow these steps to report it: Locate the Post: Find the post in question within the community.Kebab Menu: Click on the "Kebab" menu | 3 Dots, on the top right of the post.Report Inappropriate Content: Select "Report Inappropriate Content" from the menu.Submit Report: Fill out any necessary details on the form and submit your report.   Our community team will review the report and take appropriate action to ensure our community remains a valuable resource for everyone.   Thank you for helping us keep the community clean and useful!

Users online (3,625)