简体   繁体   中英

Modifying JSON Data using Logic Apps

I have over 1000 JSON files and I receive this daily. The issue I have is the language is EN and I would like it as ENGLISH. I received the JSON files via Logic App so therefore is this possible to do this in logic app.

{
  "customer": "ABCD",
  "firstname": "Bob",
  "lastname": "Doe",
  "email": "XYZ",
  "language": "EN"
}

I will also have BEL for Belgium and FR for France.

If you haven't solve this problem, please refer to the solution below, it may help your problem and maybe it is easy for us to operate it.

Since I don't know the source of your json files and where you store the files, so in my logic app I upload the json file in Azure blob storage.

First, I use "Get blob content" action in my logic app to get the content of the json file. 在此处输入图像描述

Second, initialize a variable named "jsonString" to store the json in string type. 在此处输入图像描述

Then do the replace operation by "replace()" function. 在此处输入图像描述 The full expression is

replace(variables('jsonString'), 'EN', 'ENGLISH')

Now we can get the result json what we expect. 在此处输入图像描述

If you have a lot of json files, you can use "List blobs" action to list all of the json files in blob storage and then use "Get blob content" action.

Hope it would be helpful to your problem~

Of course it depends on what else you are doing.

Far a basic example:

  1. Parsed the JSON you provided from the body of a HTTP request.
  2. Created an 'Output Data' variable to hold the updated object.
  3. Ran that through a Switch control to look at the value of the language property.
  4. If the value was 'EN' I used the setProperty function inside a Set Variable action to set my 'Output Data' variable. You can add other country matches and have the default set the variable to the original JSON
  5. Returned the 'Output Data' variable as the reponse to the request.

Here's the JSON schema for the app. I used and input data variable as well as an output, but you should be able to do it with just the output variable.

{
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "triggers": {
        "manual": {
            "type": "Request",
            "kind": "Http",
            "inputs": {
                "schema": {
                    "properties": {
                        "customer": {
                            "type": "string"
                        },
                        "email": {
                            "type": "string"
                        },
                        "firstname": {
                            "type": "string"
                        },
                        "language": {
                            "type": "string"
                        },
                        "lastname": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                }
            }
        }
    },
    "actions": {
        "Input_Data": {
            "runAfter": {},
            "type": "InitializeVariable",
            "inputs": {
                "variables": [
                    {
                        "name": "Data",
                        "type": "Object",
                        "value": "@triggerBody()"
                    }
                ]
            }
        },
        "Output_Data": {
            "runAfter": {
                "Input_Data": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable",
            "inputs": {
                "variables": [
                    {
                        "name": "Output Data",
                        "type": "Object"
                    }
                ]
            }
        },
        "Parse_JSON": {
            "runAfter": {
                "Output_Data": [
                    "Succeeded"
                ]
            },
            "type": "ParseJson",
            "inputs": {
                "content": "@variables('Data')",
                "schema": {
                    "customer": "ABCD",
                    "email": "XYZ",
                    "firstname": "Bob",
                    "language": "EN",
                    "lastname": "Doe"
                }
            }
        },
        "Response": {
            "runAfter": {
                "Switch": [
                    "Succeeded"
                ]
            },
            "type": "Response",
            "kind": "Http",
            "inputs": {
                "body": "@variables('Output Data')",
                "statusCode": 200
            }
        },
        "Switch": {
            "runAfter": {
                "Parse_JSON": [
                    "Succeeded"
                ]
            },
            "cases": {
                "Case": {
                    "case": "EN",
                    "actions": {
                        "Set_variable": {
                            "runAfter": {},
                            "type": "SetVariable",
                            "inputs": {
                                "name": "Output Data",
                                "value": "@setProperty(variables('Data'), 'language', 'English')"
                            }
                        }
                    }
                }
            },
            "default": {
                "actions": {
                    "Set_variable_2": {
                        "runAfter": {},
                        "type": "SetVariable",
                        "inputs": {
                            "name": "Output Data",
                            "value": "@variables('Data')"
                        }
                    }
                }
            },
            "expression": "@body('Parse_JSON')['language']",
            "type": "Switch"
        }
    },
    "outputs": {}
}

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