Search code examples
azureazure-logic-appssharepoint-online

In azure logic app how to extract images from a json string and save it in sharepoint


In an azure logic app I would like to save images which are given as a url in a json string. They should be saved in a sharepoint folder. The json string is very long but shortened looks somewhat like this:

{"id":"123","Person":{"Name":"Testmann","Firstname":"Tim"},"Timestamp":"2023-03-22T20:16:04.663252+01:00","Tree":{"fileId":"123","fileName":"Tree_2023_03_22_19_16_09.png","timestamp":"2023-03-22T19:16:09.082243Z","contentType":"image/png","size":123,"url":"https://path"}}

In this example the person Tim Testmann took the picture of a certain tree at a certain time. The string can contain several images, for example of the root of the tree, one doesn't know how many images will come in advance.

Does anyone know how to save the images in sharepoint? One probably has to extract all the images from the string first.

The actual json looks more like the version below. There is general info as well as several images in the same json type. How can I extract just the images and save each one in sharepoint? Thanks again!

{"id":"123","Person":{"Name":"Testmann","Firstname":"Tim"},"Timestamp":"2023-03-22T20:16:04.663252+01:00","Tree1":{"fileId":"123","fileName":"Tree_2023_03_22_19_16_09.png","timestamp":"2023-03-22T19:16:09.082243Z","contentType":"image/png","size":63865,"url":"https://path"},"Tree2":{"fileId":"1234","fileName":"Tree2_2023_03_22_19_16_09.png","timestamp":"2023-03-22T19:16:09.082243Z","contentType":"image/png","size":63865,"url":"https://path2","Tree3":{"fileId":"12345","fileName":"Tree3_2023_03_22_19_16_09.png","timestamp":"2023-03-22T19:16:09.082243Z","contentType":"image/png","size":63865,"url":"https://path3"}}

Solution

  • After reproducing from my end, I could able to achieve your requirement following the below steps.

    For demonstration purposes, I have used the below Json taking sample image url.

    {
      "id": "123",
      "Person": {
        "Name": "Testmann",
        "Firstname": "Tim"
      },
      "Timestamp": "2023-03-22T20:16:04.663252+01:00",
      "Tree": {
        "fileId": "123",
        "fileName": "Tree_2023_03_22_19_16_09.png",
        "timestamp": "2023-03-22T19:16:09.082243Z",
        "contentType": "image/png",
        "size": 123,
        "url": "https://www.thewowstyle.com/wp-content/uploads/2015/02/now-i-am-free.jpg"
      }
    }
    

    Below is the complete flow of my logic apps

    enter image description here

    Below is the parse json action schema of my logic app

    {
        "type": "object",
        "properties": {
            "id": {
                "type": "string"
            },
            "Person": {
                "type": "object",
                "properties": {
                    "Name": {
                        "type": "string"
                    },
                    "Firstname": {
                        "type": "string"
                    }
                }
            },
            "Timestamp": {
                "type": "string"
            },
            "Tree": {
                "type": "object",
                "properties": {
                    "fileId": {
                        "type": "string"
                    },
                    "fileName": {
                        "type": "string"
                    },
                    "timestamp": {
                        "type": "string"
                    },
                    "contentType": {
                        "type": "string"
                    },
                    "size": {
                        "type": "integer"
                    },
                    "url": {
                        "type": "string"
                    }
                }
            }
        }
    }
    

    Results:

    enter image description here

    enter image description here

    Update


    This can be done using Json Properties to Name Value Pair Array action. Below is something that you can do to achieve your requirement.

    enter image description here

    Flow inside True block

    enter image description here

    Results:

    enter image description here

    enter image description here

    Code-view of my logic app

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "For_each": {
                    "actions": {
                        "Compose_2": {
                            "inputs": "@items('For_each')",
                            "runAfter": {},
                            "type": "Compose"
                        },
                        "Condition": {
                            "actions": {
                                "Create_file": {
                                    "inputs": {
                                        "body": "@body('HTTP')",
                                        "host": {
                                            "connection": {
                                                "name": "@parameters('$connections')['sharepointonline']['connectionId']"
                                            }
                                        },
                                        "method": "post",
                                        "path": "/datasets/@{encodeURIComponent(encodeURIComponent('https://<>.sharepoint.com/<>/<>'))}/files",
                                        "queries": {
                                            "folderPath": "/Shared Documents/urlImages",
                                            "name": "@{items('For_each')['propertyValue'][1]['propertyValue']}",
                                            "queryParametersSingleEncoded": true
                                        }
                                    },
                                    "runAfter": {
                                        "HTTP": [
                                            "Succeeded"
                                        ]
                                    },
                                    "runtimeConfiguration": {
                                        "contentTransfer": {
                                            "transferMode": "Chunked"
                                        }
                                    },
                                    "type": "ApiConnection"
                                },
                                "HTTP": {
                                    "inputs": {
                                        "method": "GET",
                                        "uri": "@{items('For_each')['propertyValue'][5]['propertyValue']}"
                                    },
                                    "runAfter": {},
                                    "type": "Http"
                                }
                            },
                            "expression": {
                                "and": [
                                    {
                                        "not": {
                                            "equals": [
                                                "@items('For_each')['propertyName']",
                                                "id"
                                            ]
                                        }
                                    },
                                    {
                                        "not": {
                                            "equals": [
                                                "@items('For_each')['propertyName']",
                                                "Person"
                                            ]
                                        }
                                    },
                                    {
                                        "not": {
                                            "equals": [
                                                "@items('For_each')['propertyName']",
                                                "Timestamp"
                                            ]
                                        }
                                    }
                                ]
                            },
                            "runAfter": {
                                "Compose_2": [
                                    "Succeeded"
                                ]
                            },
                            "type": "If"
                        }
                    },
                    "foreach": "@body('Json_Properties_to_Name_Value_Pair_Array')",
                    "runAfter": {
                        "Json_Properties_to_Name_Value_Pair_Array": [
                            "Succeeded"
                        ]
                    },
                    "type": "Foreach"
                },
                "Json_Properties_to_Name_Value_Pair_Array": {
                    "inputs": {
                        "body": {
                            "data": "@json(triggerBody())"
                        },
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['advanceddataoperatio_1']['connectionId']"
                            }
                        },
                        "method": "post",
                        "path": "/JsonPropertiesToNameValuePairArray"
                    },
                    "runAfter": {},
                    "type": "ApiConnection"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "manual": {
                    "inputs": {
                        "schema": {}
                    },
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {
                    "advanceddataoperatio_1": {
                        "connectionId": "/subscriptions/<subid>/resourceGroups/<rg>/providers/Microsoft.Web/connections/advanceddataoperatio-1",
                        "connectionName": "advanceddataoperatio-1",
                        "id": "/subscriptions/<subid>/providers/Microsoft.Web/locations/eastus/managedApis/advanceddataoperatio"
                    },
                    "sharepointonline": {
                        "connectionId": "/subscriptions/<subid>/resourceGroups/<rg>/providers/Microsoft.Web/connections/sharepointonline",
                        "connectionName": "sharepointonline",
                        "id": "/subscriptions/<subid>/providers/Microsoft.Web/locations/eastus/managedApis/sharepointonline"
                    }
                }
            }
        }
    }