架构未验证

时间:2013-07-16 20:15:02

标签: python json jsonschema

我正在使用JSON模式,我正在尝试使用python JSON模块来验证针对模式的一些JSON I输出。

我收到以下错误,表明架构本身没有验证:

validation
Traceback (most recent call last):
  File "/private/var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup At Startup/gc_aicep-395698294.764.py", line 814, in <module>
    validate(entry,gc_schema)
  File "/Library/Python/2.7/site-packages/jsonschema/validators.py", line 468, in validate
    cls(schema, *args, **kwargs).validate(instance)
  File "/Library/Python/2.7/site-packages/jsonschema/validators.py", line 117, in validate
    raise error
jsonschema.exceptions.ValidationError: ({'website': 'www.stepUp.com', 'bio': '', 'accreditations': {'portfolio': '', 'certifications': [], 'degrees': {'degree_name': [], 'major': '', 'institution_name': '', 'graduation_distinction': '', 'institution_website': ''}}, 'description': 'A great counselor', 'photo': '', 'twitter': '', 'additionaltext': '', 'linkedin': '', 'price': {'costtype': [], 'costrange': []}, 'phone': {'phonetype': [], 'value': '1234567891'}, 'facebook': '', 'counselingtype': [], 'logourl': '', 'counselingoptions': [], 'linkurl': '', 'name': {'first_name': u'Rob', 'last_name': u'Er', 'middle_name': u'', 'title': u''}, 'email': {'emailtype': [], 'value': ''}, 'languages': 'english', 'datasource': {'additionaltext': '', 'linkurl': '', 'linktext': '', 'logourl': ''}, 'linktext': '', 'special_needs_offer': '', 'company': 'Step Up', 'location': {'city': 'New York', 'zip': '10011', 'locationtype': '', 'state': 'NY', 'address': '123 Road Dr', 'loc_name': '', 'country': 'united states', 'geo': ['', '']}},) is not of type 'object'

validationError消息表明冒号后面的内容不是有效的JSON对象,我想,但我无法弄清楚为什么它不会。

如果用双引号替换单引号并且从任一方删除基本括号,则此JSON使用类似JSON Lint的验证器进行验证。


名称前面的'u'被标记为可能的错误。

这是输出名称的代码:

name = HumanName(row['name'])
first_name = name.first
middle_name = name.middle
last_name = name.last
title = name.title
full_name = dict(first_name=first_name, middle_name=middle_name, last_name=last_name, title=title)    

使用以下命令将名称插入JSON:

gc_ieca = dict( 

name = full_name,
twitter = twitter, 

logourl = logourl,
linktext = linktext,
linkurl = linkurl,
additionaltext = additionaltext,
datasource = datasource,

phone=phone,
email = email,

price = price,
languages = languages,

special_needs_offer = special_needs_offer,

# location
location = location,

accreditations = accreditations,

website = website

),

2 个答案:

答案 0 :(得分:3)

这不是ValidationError表示的内容。它表明验证失败:),而不是JSON无效(jsonschema甚至不处理JSON,它处理反序列化的JSON,即Python对象,这里是dict)。如果JSON无效,则在您拨打json.load时会出现错误。

它失败的原因是因为它实际上不是一个对象,它是一个单元素,一个对象的元组,所以它实际上是无效的。为什么它是一个元组是你的代码中的一个错误(我看到你最后有一个迷路逗号)。

(仅供参考,u前缀是因为它们是unicode文字,单引号是因为它是repr的{​​{1}},与JSON无关)。

答案 1 :(得分:2)

我在这里看到两个潜在的问题:

  1. 使用单引号。严格地说,json spec要求对字符串使用双引号。你的最后一点意味着这不是你的问题,但值得一提的是,检查修复#2是否无法解决问题。
  2. 名称的值:这些列为u'...',这是无效的json。 u的使用必须后跟4个十六进制数字,并且应该在\转义符之后落在字符串周围的双引号内。