简体   繁体   English

使用逻辑应用修改 JSON 数据

[英]Modifying JSON Data using Logic Apps

I have over 1000 JSON files and I receive this daily.我有超过 1000 个 JSON 文件,我每天都会收到这个文件。 The issue I have is the language is EN and I would like it as ENGLISH.我遇到的问题是语言是 EN,我希望它是 ENGLISH。 I received the JSON files via Logic App so therefore is this possible to do this in logic app.我通过逻辑应用程序收到了 JSON 文件,因此可以在逻辑应用程序中执行此操作。

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

I will also have BEL for Belgium and FR for France.我还将有比利时的 BEL 和法国的 FR。

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.由于我不知道您的 json 文件的来源以及存储文件的位置,因此在我的逻辑应用程序中,我将 json 文件上传到 Azure blob 存储中。

First, I use "Get blob content" action in my logic app to get the content of the json file.首先,我在我的逻辑应用程序中使用“获取 blob 内容”操作来获取 json 文件的内容。 在此处输入图像描述

Second, initialize a variable named "jsonString" to store the json in string type.其次,初始化一个名为“jsonString”的变量,以字符串类型存储json。 在此处输入图像描述

Then do the replace operation by "replace()" function.然后通过“replace()”function进行替换操作。 在此处输入图像描述 The full expression is完整的表达是

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

Now we can get the result json what we expect.现在我们可以得到我们期望的结果 json。 在此处输入图像描述

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.如果您有很多 json 文件,您可以使用“列出 blob”操作列出 blob 存储中的所有 json 文件,然后使用“获取 blob 内容”操作。

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.解析了您从 HTTP 请求的正文中提供的 JSON。
  2. Created an 'Output Data' variable to hold the updated object.创建了一个“输出数据”变量来保存更新的 object。
  3. Ran that through a Switch control to look at the value of the language property.通过 Switch 控件运行它以查看语言属性的值。
  4. If the value was 'EN' I used the setProperty function inside a Set Variable action to set my 'Output Data' variable.如果值为“EN”,我在 Set Variable 操作中使用setProperty function 来设置我的“输出数据”变量。 You can add other country matches and have the default set the variable to the original JSON您可以添加其他国家/地区匹配项并将默认变量设置为原始 JSON
  5. Returned the 'Output Data' variable as the reponse to the request.返回“输出数据”变量作为对请求的响应。

Here's the JSON schema for the app.这是应用程序的 JSON 架构。 I used and input data variable as well as an output, but you should be able to do it with just the output variable.我使用并输入了数据变量以及 output,但您应该可以只使用 output 变量来完成。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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