什么JSON对关键字'anyOf'有效

时间:2013-07-12 15:28:10

标签: json jsonschema

我想知道是否有人可以使用'anyOf'关键字给出一个对于模式有效的JSON示例?我正在尝试编写一些将在python中输出有效JSON的代码。

例如,如果您的架构看起来像这样:

"anyOf": {
       "category_0": {
                "main": { "enum": ["learning"]},
                "sub":  { "enum": ["ADD/ADHD", "dyslexia", "general learning disability", "language disorder", "intellectual giftedness", "other"]}
                         },
       "category_1": {
                "main": { "enum": ["mental"]},
                "sub":  { "enum": ["down's syndrome", "asperger's syndrome", "autism", "other"]}
                 },
       "category_2": {
                "main": { "enum": ["behavioral"]}
                 },
       "category_3": {
                "main": { "enum": ["medical"]},
                "sub":  { "enum": ["diabetes", "allergies", "eating disorders", "chronic illness", "other"]}
                 },
       "category_4": {
                "main": { "enum": ["physical"]},
                "sub":  { "enum": ["blind", "deaf", "cerebral palsy", "other"]}
                    }
                 }

什么JSON有效?

我用于在python中输出JSON的代码是:

main = row.get('main', [])
sub = row.get('sub',[])
category = dict(main = main, sub=sub)
special_needs = category

1 个答案:

答案 0 :(得分:3)

您的JSON无效,因为字符串不能位于最外层的数组或对象之外。

最低有效的JSON如下所示:{}。您可以通过将您在此处显示的内容放在其中来使您的JSON有效:

{
  "anyOf": {
    "category_0": {
      "main": {
        "enum": [
          "learning"
        ]
      },
      "sub": {
        "enum": [
          "ADD/ADHD",
          "dyslexia",
          "general learning disability",
          "language disorder",
          "intellectual giftedness",
          "other"
        ]
      }
    },
    "category_1": {
      "main": {
        "enum": [
          "mental"
        ]
      },
      "sub": {
        "enum": [
          "down's syndrome",
          "asperger's syndrome",
          "autism",
          "other"
        ]
      }
    },
    "category_2": {
      "main": {
        "enum": [
          "behavioral"
        ]
      }
    },
    "category_3": {
      "main": {
        "enum": [
          "medical"
        ]
      },
      "sub": {
        "enum": [
          "diabetes",
          "allergies",
          "eating disorders",
          "chronic illness",
          "other"
        ]
      }
    },
    "category_4": {
      "main": {
        "enum": [
          "physical"
        ]
      },
      "sub": {
        "enum": [
          "blind",
          "deaf",
          "cerebral palsy",
          "other"
        ]
      }
    }
  }
}