追加JSON结构的脚本

时间:2019-02-04 16:07:06

标签: python bash shell

我有一个JSON结构,需要附加一些代码。我尝试使用SED和bash,它们仅附加在字符串或文件的末尾,而不附加在结构的末尾。

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "required": [
    "accounts"
  ],
  "accounts": {
    "required": "account",
    "properties": {
      "account": {
        "type": "array",
        "minItems": 1,
        "maxItems": 999,
        "required": [
          "scheme",
          "accountType",
          "accountSubType"
        ],
        "items": {
          "type": "object",
          "properties": {
            "scheme": {
              "description": "scheme",
              "type": "object",
              "required": [
                "schemeName",
                "identification"
              ],
              "properties": {
                "schemeName": {
                  "type": "string",
                  "maxLength": 40
                },
                "identification": {
                  "type": "string",
                  "maxLength": 256
                },
                "name": {
                  "type": "string",
                  "maxLength": 70
                },
                "secondaryIdentification": {
                  "type": "string",
                  "maxLength": 35
                }
              }
            },
            "currency": {
              "type": "string",
              "format": "iso-4217",
              "pattern": "^[A-Z]{3,3}$",
              "maxLength": 3,
              "example": "EUR"
            },
            "accountType": {
              "type": "string"
            },
            "accountSubType": {
              "type": "string",
              "maxLength": 35
            }
          }
        }
      }
    }
  }
}

我想将以上内容更新为

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "required": [
        "accounts"
    ],
    "accounts": {
        "required": "account",
        "properties": {
            "account": {
                "type": "array",
                "minItems": 1,
                "maxItems": 999,
                "required": [
                    "scheme",
                    "accountType",
                    "accountSubType"
                ],
                "items": {
                    "type": "object",
                    "properties": {
                        "scheme": {
                            "description": "scheme",
                            "type": "object",
                            "required": [
                                "schemeName",
                                "identification"
                            ],
                            "properties": {
                                "schemeName": {
                                    "type": "string",
                                    "maxLength": 40
                                },
                                "identification": {
                                    "type": "string",
                                    "maxLength": 256
                                },
                                "name": {
                                    "type": "string",
                                    "maxLength": 70
                                },
                                "secondaryIdentification": {
                                    "type": "string",
                                    "maxLength": 35
                                }
                            },
                            "additionalProperties": false
                        },
                        "currency": {
                            "type": "string",
                            "format": "iso-4217",
                            "pattern": "^[A-Z]{3,3}$",
                            "maxLength": 3,
                            "example": "EUR"
                        },
                        "accountType": {
                            "type": "string"
                        },
                        "accountSubType": {
                            "type": "string",
                            "maxLength": 35
                        }
                    },
                    "additionalProperties": false
                }
            }
        },
        "additionalProperties": false
    }
}

区别在于每个“属性”部分的末尾。我已经用"additionalProperties": false

申请了

有没有一种方法可以通过脚本来检查并附加所有属性?

2 个答案:

答案 0 :(得分:1)

您可以使用jq进行此操作(需要jq 1.6,因为它使用walk()函数来遍历整个结构)

$ jq 'walk(if type == "object" and has("properties") then . + { additionalProperties: false } else . end)' your.json

答案 1 :(得分:0)

“ additionalProperties”在“ properties”之后还是之前是否重要? 如果没有,您可以使用sed在对象“属性”之前添加“ additionalProperties”,如下所示:

SelectedValue

有了你,你会得到

productsBindigSource - CategoryId