简体   繁体   中英

Difficulties accessing json values in request body of powershell azure function

I have an Powershell Azure function that receives a (json) string as request body. I need to access the values of XMLA_String , WorkspaceId , and DatasetId in order to set them to variables. Please let me know how

The following body is received:

"{\"XMLA_String\":'{\"refresh\":{\"type\":\"full\",\"objects\":[{\"database\":\"<db>\",\"table\":\"<tab>\"}]}}',
\"WorkspaceId\":'<workspaceid>',
\"DatasetId\":'<datasetid>'}"

Assuming that the line breaks in your sample JSON are just a formatting artifact, it looks like you're getting doubly JSON-encoded data, strangely.

Therefore, try applying ConvertFrom-Json twice (which you've since confirmed solved your problem):

# Sample response received.
$json = @'
"{\"XMLA_String\":'{\"refresh\":{\"type\":\"full\",\"objects\":[{\"database\":\"<db>\",\"table\":\"<tab>\"}]}}',\"WorkspaceId\":'<workspaceid>',\"DatasetId\":'<datasetid>'}"
'@

$json | ConvertFrom-Json | ConvertFrom-Json

The above outputs a [pscustomobject] instance with properties XMLA_String , WorkspaceId , and DatasetId :

XMLA_String                                                                 WorkspaceId   DatasetId
-----------                                                                 -----------   ---------
{"refresh":{"type":"full","objects":[{"database":"<db>","table":"<tab>"}]}} <workspaceid> <datasetid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM