Скрипт для добавления структуры JSON - PullRequest
0 голосов
/ 04 февраля 2019

У меня есть структура 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 голосов
/ 05 февраля 2019

Вы можете сделать это с помощью jq (требуется jq 1.6, потому что он использует функцию walk() для обхода всей структуры):

$ jq 'walk(if type == "object" and has("properties") then . + { additionalProperties: false } else . end)' your.json
0 голосов
/ 04 февраля 2019

Имеет ли значение, если "AdditionalProperties" идет после или перед "свойствами"?Если нет, вы можете использовать sed, чтобы добавить «дополнительные свойства» перед «свойствами» объекта, например:

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
            }
          }
        }
      }
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...