Djbril
Djbril

Reputation: 895

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.

Upvotes: 0

Views: 6268

Answers (2)

Hury Shen
Hury Shen

Reputation: 15734

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. enter image description here

Second, initialize a variable named "jsonString" to store the json in string type. enter image description here

Then do the replace operation by "replace()" function. enter image description here The full expression is

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

Now we can get the result json what we expect. enter image description here

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~

Upvotes: 3

PerfectlyPanda
PerfectlyPanda

Reputation: 3466

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": {}
}

Upvotes: 0

Related Questions