Groovy脚本将json字符串合并到json文件中

2020-02-15 json merge append

我们想要编写可解析JsonfileA并提取数据并合并到JsonfileB中的groovy脚本。 我们正在关注JsonfileA。我们需要从“路径”节点中提取数据。我们正在使用JsonSlurper来解析文件并从“路径”节点读取数据

JsonfileA
==========
{
  "swagger": "2.0",
  "basePath": "/abc",
  "paths": {
    "/api/abcBusiness/cussShipTos": {
      "post": {
        "tags": [
          "abcBusiness"
        ],
        "operationId": "cussShipTos",
        "produces": [
          "application/json"
        ]
      }
    }
  },
  "definitions": {
    "cussShipTosInput": {
      "type": "object",
      "properties": {
        "geo": {
          "type": "string"
        }
      }
    }
  }
}

我们跟随着JsonfileB,我们想将JsonfileA的“路径”节点数据附加到“路径”节点JsonfileB中 而且我们想保存JsonfileB

JsonfileB
===========
{
  "x-generator": "NSwag v13.0.5.0 (NJsonSchema v10.0.22.0 (Newtonsoft.Json v11.0.0.0))",
  "swagger": "2.0",
  "paths": {
    "/api/Bancco/JobSpec": {
      "post": {
        "tags": [
          "Bancco"
        ],
        "operationId": "JobSpec"
      }
    }
  },
  "definitions": {
    "cusatestResponse": {
      "type": "object",
      "description": "Model class object with the values that would be sent as response from the Service",
      "properties": {
        "TransactionDetail": {
          "$ref": "#/definitions/cusatestTransactionDetail"
        }
      }
    }
  }
}

将JosonfileA绑定到“ paths”节点下的JsonfileB之后,结果应如下所示。 预期结果

  "x-generator": "NSwag v13.0.5.0 (NJsonSchema v10.0.22.0 (Newtonsoft.Json v11.0.0.0))",
  "swagger": "2.0",
  "paths": {
    "/api/Bancco/JobSpec": {
      "post": {
        "tags": [
          "Bancco"
        ],
        "operationId": "JobSpec"
      }
    },
    "/api/abcBusiness/cussShipTos": {
      "post": {
        "tags": [
          "abcBusiness"
        ],
        "operationId": "cussShipTos",
        "produces": [
          "application/json"
        ]
      }
    }
  },
  "definitions": {
    "cusatestResponse": {
      "type": "object",
      "description": "Model class object with the values that would be sent as response from the Service",
      "properties": {
        "TransactionDetail": {
          "$ref": "#/definitions/cusatestTransactionDetail"
        }
      }
    }
  }
}

Answers

Related