附加JSON结构的脚本

2024-09-22 16:40:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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来形容它

有没有一种方法可以通过一个我可以检查并附加所有属性的脚本来实现这一点?你知道吗


Tags: stringobjectschematyperequiredaccountpropertiesscheme
2条回答

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

sed -E 's/([[:space:]]*)"properties": {/\1"additionalProperties": false,|\1"properties": {/g'| tr '|' '\n'

有了你,你就会得到

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "required": [
    "accounts"
  ],
  "accounts": {
    "required": "account",
    "additionalProperties": false,
    "properties": {
      "account": {
        "type": "array",
        "minItems": 1,
        "maxItems": 999,
        "required": [
          "scheme",
          "accountType",
          "accountSubType"
        ],
        "items": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "scheme": {
              "description": "scheme",
              "type": "object",
              "required": [
                "schemeName",
                "identification"
              ],
              "additionalProperties": false,
              "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
            }
          }
        }
      }
    }
  }
}

您可以使用jq(需要jq1.6,因为它使用walk()函数遍历整个结构):

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

相关问题 更多 >