goldisfine
goldisfine

Reputation: 4850

Schema not validating

I'm working with a JSON schema and I'm trying to use the python JSON module to validate some JSON I output against the schema.

I get the following error, indicating that the Schema itself is not validating:

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'

The validationError message indicates that what follows the colon is not a valid JSON object, I think, but I can't figure out why it would not be.

This JSON validates using a validator like JSON Lint if you replace the single quotation marks with double and remove the basic parentheses from either side.


The 'u' before the name has been flagged as a possible bug.

This is the code which outputs the name:

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)    

name is inserted into the JSON using the following:

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

),

Upvotes: 0

Views: 2912

Answers (2)

Julian
Julian

Reputation: 3429

That isn't what a ValidationError indicates. It indicates that validation failed :), not that the JSON is invalid (jsonschema doesn't even deal with JSON, it deals with deserialized JSON i.e. Python objects, here a dict). If the JSON were invalid you'd get an error when you called json.load.

The reason it's failing is because that in fact is not an object, it's a tuple with a single element, an object, so it is in fact invalid. Why it is a tuple is a bug in your code (you have a stray comma at the end there I see).

(And FYI, the u prefixes are because those are unicode literals, and the single quotes are because that's the repr of str, nothing to do with JSON).

Upvotes: 3

Jeffrey Blake
Jeffrey Blake

Reputation: 9709

I see two potential issues here:

  1. Use of single quotes. Strictly speaking, the json spec calls for a use of double-quotes for strings. Your final note sort of implies this is not your issue, however it is worth mentioning, as something to check if fixing #2 doesn't resolve the problem.
  2. Values for name: these are listed as u'...' which is not valid json. Use of u must be followed by 4 hex digits, and should fall within the double-quotes surrounding a string, after a \ escape character.

Upvotes: 2

Related Questions