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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to efficiently con...
Power Automate
Suggested Answer

How to efficiently convert SharePoint search rest query into array with xpath/xml and filter array?

(1) ShareShare
ReportReport
Posted on by 22
 
Apologies, this may not be as detailed as it was before -- this new forum really is terrible, and after I hit enter it emptied all the content from the input box.  In any case, I am trying to figure out the best way to extract the data I want from a SharePoint Search Rest query.  Because the output's JSON structure is basically a bunch of nested object arrays with the nondescript keys for the properties I selected [i.e. instead of Title being a Key and the value being 'Doc Title', I get Key:Title, Value: Pages where Key is always the property I selected and Value is always the property value which makes it difficult to select content I want effectively through any normal means), I am using xpath.  
 
I am, however, a bit confused as to how I can do the following:
a.) transform the entire array from the Select output to a string versus base64
b.) filter out the junk data that I do not want from the SP rest call
c.) join the dataset together so it creates one array for each result with the following info as Keys:   Title, URL, ParentLink
 
 
Here is my flow setup.
 
1.) Send a HTTP req to SharePoint
URI: _api/search/query?queryText='contentclass:STS_List_850 AND Path:mysharepoint.com/*'&selectproperties='Title, Path, ParentLink'&rowlimit=500
 
The output that it renders is this [this is a truncated version]
 
 
"Table": {
"__metadata": { "type": "SP.SimpleDataTable"},
                            "Rows": {
                                "results": [
                                    {
                                        "__metadata": {
                                            "type": "SP.SimpleDataRow"
                                        },
                                        "Cells": {
                                            "results": [
                                                {
                                                    "__metadata": {
                                                        "type": "SP.KeyValue"
                                                    },
                                                    "Key": "Title",
                                                    "Value": "Pages",
                                                    "ValueType": "Edm.String"
                                                },
                                                {
                                                    "__metadata": {
                                                        "type": "SP.KeyValue"
                                                    },
                                                    "Key": "Path",
                                                    "Value": "https://my.sharepoint.com/SiteAssets/Pages",
                                                    "ValueType": "Edm.String"
                                                },
                                                {
                                                    "__metadata": {
                                                        "type": "SP.KeyValue"
                                                    },
                                                    "Key": "ParentLink",
                                                    "Value": "https://my.sharepoint.com/SiteAssets/Forms/AllItems.aspx",
                                                    "ValueType": "Edm.String"
                                                },
2. Compose (convert the output above into xml with root
Compose:  xml(json(concat('{"root":{"items":', body(output(SendRequest)[d][results][etc][etc],'}}')))
 
3. Select (create a table of the Keys and Values)
Field:  xpath(outputs('Compose'), '//Key'))
Value: xpath(outputs('Compose'), '//Value))
 
4. For Each. (this is where things get tricky for me.  it appears that I have to do a For loop in order to convert everything into string from base64; and more importantly, that I have to do each of my keys - field and value -- one at a time??)
Parameters/output from previous step:   outputs('Select')['body'][0][Field Type]
Compose[2]:  base64ToString(item()?['$content'])
 
 
 
 
The following produces what I want, technically.  There are no errors.  But I feel as if this way is not efficient and could cause significant delay.  Additionally, I'm not sure how to properly filter out the junk data nor am I sure how to join 'Field' and 'Value' into an array to make my csv table.  
I have the same question (0)
  • Suggested answer
    Haque Profile Picture
    3,653 on at
     
     
    Technically you are in right track except how to join and narrow down the array object to take to the csv table.
     
    In my POV,  xml(json(concat('{"root":{"items":', body(output(SendRequest)[d][results][etc][etc],'}}'))) - this much better than navigating attributes with Select action like if(equals(item()['Rows']?['Cells']?['results']?['Key'], 'Title'),'goodtitle','badtitle') - just for an example.
     
     
    If you want to avoid xml function, you need to introduce a lot of logic like multiple Apply for each and Select and Filter Array function. 
    Can you please post the object you have least narrowed down, I meant "The following produces what I want, technically.  There are no errors." up until here?
     
     
    What I realised is you want to come in a place where you have stripped off all the unnecessary data  from the object, something like:
     
      
    [
      {
        "Title": "Pages",
        "URL": "https://my.sharepoint.com/SiteAssets/Pages",
        "ParentLink": "https://my.sharepoint.com/SiteAssets/Forms/AllItems.aspx"
      },
      {
        "Title": "Asstes",
        "URL": "https://my.sharepoint.com/SiteAssets/Assets",
        "ParentLink": "https://my.sharepoint.com/SiteAssets/Forms/AllItems.aspx"
      },
      
      {
        "Title": "Asstes",
        "URL": "https://my.sharepoint.com/SiteAssets/Assets",
        "ParentLink": "https://my.sharepoint.com/SiteAssets/Forms/AllItems.aspx"
      }
    ]
     
     
    From where you can send email or store in SP library.
     
    Please post me the object where you last stand so that I can help to narrow down to be a csv.
     
     

    I am sure some clues I tried to give. If these clues help to resolve the issue brought you by here, please don't forget to check the box Does this answer your question? At the same time, I am pretty sure you have liked the response!
     
  • Chriddle Profile Picture
    8,708 Super User 2026 Season 1 on at
    Are you looking for this?
     
     
    From:
    xpath(
    	xml(
    		addProperty(
    			json('{}'),
    			'Root',
    			addProperty(
    				json('{}'),
    				'Item',
    				outputs('Compose').Table.Rows.results
    			)
    		)
    	),
    	'//Item/Cells/results'
    )
    Map:
    json(item()).results
     
     
     
    From there use another Select:
    From:
    chunk(body('Select'), 3)
    Map Key 0:
    item()[0].Key
    Map Value 0:
    item()[0].Value
    Same for the other item/value pairs
     
  • Chriddle Profile Picture
    8,708 Super User 2026 Season 1 on at
    Or just one Select:
    From:
    chunk(
    	xpath(
    		xml(
    			addProperty(
    				json('{}'),
    				'Root',
    				addProperty(
    					json('{}'),
    					'Item',
    					outputs('Compose').Table.Rows.results
    				)
    			)
    		),
    		'//Item/Cells/results/Key/text() | //Item/Cells/results/Value/text()'
    	),
    	6
    )
    Map:
    json(
    	concat(
    		'{',
    		'"', item()[0], '":', '"', item()[1], '",',
    		'"', item()[2], '":', '"', item()[3], '",',
    		'"', item()[4], '":', '"', item()[5], '",',
    		
    		'}'
    	)
    )
  • Chriddle Profile Picture
    8,708 Super User 2026 Season 1 on at
    Looks like you don't need xpath:
    From:
    outputs('Compose').Table.Rows.results
    Map Key 0:
    item().Cells.results[0].Key
    Map Value 0:
    item().Cells.results[0].Value
    Same with the other key/value pairs
     
  • IPhuckingHateMS Profile Picture
    22 on at
    @Haque

    I meant that all of the above produces no errors, but it isn't what I want.  You are absolutely correct; I want to strip things down, but i think everyone is missing the point that the JSON object array is truncated... This is the info I want, but each result produces like 20 objects all with Key/Value/ValueType keys, so I think that's where the disconnect for me is.  I'd like to know how to strip away all the junk except for those.   
     
    And it's only an example; I am doing a whole site collection search of Pages libraries and so I may have like 300 hundred results.
  • IPhuckingHateMS Profile Picture
    22 on at
    @Chriddle
     
    Thanks for your input!  I'm aware I could do this with dot notation/identifying the number of it with a Key, however, that's not necessarily realistic when I am getting hundreds of results back from my search query.  I want it to be dynamic;  the sample data provided is just an example but it is 1) a truncated version of the output as there are many results arrays in the cels, and 2) unfortunately those three properties are what I'd like but with each array you do get 40 other properties that are more system generated.
  • Haque Profile Picture
    3,653 on at
     
    When you mention "each result produces like 20 objects" you meant it under each Cell, right? If this is the case, I am guessing there are multiple rows as well?
     
    Under Table, the combination is Rows/reuslts/Cells/result - the file response format comes this way? to be very specific is  this something like below?
     
     
     
    Also, you mentioned we are missing the point that the JSON object array is truncated. Would be happy if you could visually point me to where it is happening. I really missed out on this!
     
    Finally, if it is not that tough in terms of data security, could you please send one complete object signature attaching a file?
     
  • Chriddle Profile Picture
    8,708 Super User 2026 Season 1 on at
    I've added a second table row and another cell result (with Key: not_needed) to the JSON:
    {
    	"Table": {
    		"__metadata": { "type": "SP.SimpleDataTable"},
    		"Rows": {
    			"results": [
    				{
    					"__metadata": {
    						"type": "SP.SimpleDataRow"
    					},
    					"Cells": {
    						"results": [
    							{
    							    "__metadata": {
    								"type": "SP.KeyValue"
    							    },
    							    "Key": "Title",
    							    "Value": "Pages",
    							    "ValueType": "Edm.String"
    							},
    							{
    							    "__metadata": {
    								"type": "SP.KeyValue"
    							    },
    							    "Key": "Path",
    							    "Value": "https://my.sharepoint.com/SiteAssets/Pages",
    							    "ValueType": "Edm.String"
    							},
    							{
    							    "__metadata": {
    								"type": "SP.KeyValue"
    							    },
    							    "Key": "not_needed",
    							    "Value": "I don't care",
    							    "ValueType": "Edm.String"
    							},
    							{
    							    "__metadata": {
    								"type": "SP.KeyValue"
    							    },
    							    "Key": "ParentLink",
    							    "Value": "https://my.sharepoint.com/SiteAssets/Forms/AllItems.aspx",
    							    "ValueType": "Edm.String"
    							}
    						]
    					}
    				},
    				{
    					"__metadata": {
    						"type": "SP.SimpleDataRow"
    					},
    					"Cells": {
    						"results": [
    							{
    							    "__metadata": {
    								"type": "SP.KeyValue"
    							    },
    							    "Key": "Title",
    							    "Value": "Pages_1",
    							    "ValueType": "Edm.String"
    							},
    							{
    							    "__metadata": {
    								"type": "SP.KeyValue"
    							    },
    							    "Key": "Path",
    							    "Value": "https://my.sharepoint.com/SiteAssets/Pages_1",
    							    "ValueType": "Edm.String"
    							},
    							{
    							    "__metadata": {
    								"type": "SP.KeyValue"
    							    },
    							    "Key": "not_needed",
    							    "Value": "I don't care",
    							    "ValueType": "Edm.String"
    							},
    							{
    							    "__metadata": {
    								"type": "SP.KeyValue"
    							    },
    							    "Key": "ParentLink",
    							    "Value": "https://my.sharepoint.com/SiteAssets/Forms/AllItems.aspx_1",
    							    "ValueType": "Edm.String"
    							}
    						]
    					}
    				}				
    			]
    		}
    	}
    }
     
    The XPath expression in the From of the Select now contains a definition of which cells are needed.
    From:
    chunk(
    	xpath(
    		xml(
    			addProperty(
    				json('{}'),
    				'Root',
    				addProperty(
    					json('{}'),
    					'Item',
    					outputs('Compose').Table.Rows.results
    				)
    			)
    		),
    		'//Item/Cells/results[Key="Title" or Key="Path" or Key="ParentLink"]/Key/text() | //Item/Cells/results[Key="Title" or Key="Path" or Key="ParentLink"]/Value/text()'
    	),
    	6
    )
    Map:
    json(
    	concat(
    		'{',
    		'"', item()[0], '":', '"', item()[1], '",',
    		'"', item()[2], '":', '"', item()[3], '",',
    		'"', item()[4], '":', '"', item()[5], '",',
    		
    		'}'
    	)
    )
     
  • IPhuckingHateMS Profile Picture
    22 on at
    @Chriddle

    Hi there, thank you so much!  I think that will cover it.  My other idea was to create my own array and maybe did a Condition to check if the key was either of the properties, but this is so much cleaner and exactly what I was looking for.  Thanks again!

    EDIT:  Due to chunk requiring a set integer and since the data set is dynamic, this doesn't work completely.  But adding the filter to select which keys I need is great to incorporate in my current formula.  I think I can then add everything to an array variable.
  • IPhuckingHateMS Profile Picture
    22 on at
    @Haque

    Hi!  It is truncated because I actually didn't paste the entire response in the window -- the output is massive so I showed a sample of how the data I want is actually structured.  Additionally, I am posting here on a diff laptop than where I'm working since that info is secured and I can't actually access external sites on that network.  [Otherwise I'd save us all some pain and do screenshots lol].
     
    The hang up for me is that I didn't know how to get rid of the extra data..  The cells you all see are just a sample, but there are other Keys such as IdentitySiteId, ListId etc that go on and on.   
     
    I think Chriddle helped in that I can use that code to silo out the data I want in my current Select action [albeit altered].   And then once that's the case I may be able to just use a couple of compose actions to structure an array.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Valantis Profile Picture

Valantis 377

#2
11manish Profile Picture

11manish 279

#3
David_MA Profile Picture

David_MA 234 Super User 2026 Season 1

Last 30 days Overall leaderboard