json - 生成文件并替换生成文件中模板文件中的字符串

标签 json bash sed jq auto-generate

我有一个 JSON 文件:

{
    "header": {
        "uuid": "c578592a-a751-4993-9060-53f488597e59",
        "timestamp": 1522938800,
        "productionDateTime": "2018-04-05T14:33:20.000+00:00",
        "producers": {
            "operator": null,
            "application": {
                "com.example": {
                    "id": {
                        "string": "1"
                    },
                    "name": {
                        "string": "Test"
                    }
                }
            },
            "station": null,
            "equipment": null,
            "partner": null,
            "flow": null
        },
        "correlationContext": {
            "array": [{
                "correlationId": "98440498-3104-479e-9c99-f4449ba8f4b6",
                "correlationDateTime": {
                    "string": "2018-04-05T14:30:39.000+00:00"
                }
            }]
        }
    },
}

我想创建 n 个文件来复制 JSON 文件,但具有随机的correlationId 和correlationDateTime。

您有什么建议或建议吗?非常感谢!

最佳答案

使用jq并将生成适当的随机值作为读者的练习:

$ jq --arg cid "foo" --arg cdt "bar" '
    .header.correlationContext.array[0]={
        correlationId: $cid,
        correlationDateTime: {string: $cdt}
     }' tmp.json

关于json - 生成文件并替换生成文件中模板文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49675733/

相关文章:

c# - 使用 { get; 将 .json 反序列化为 C#放; }(牛顿软件)

java - 根据对象属性动态定义XML标签

ios - 在 JSON 字符串中添加反斜杠 "\"用于 swift 中的 Web 服务

ios - 在快速加载页面之前等待 json 数据被下载

linux - rm : cannot remove: Permission denied

bash - 使用 bash 脚本以自然顺序列出文件夹中的文件

linux - 在平面文件中用单引号括起一个字段

bash - 删除 csv 中超过 X 列的行

database - 从空格分隔的文件中剪切特定列

linux - Sed 遍历文件并替换占位符变量