0

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 ), 
1
  • Perhaps add in the code where these fields are inserted to the json? Commented Jul 16, 2013 at 20:53

2 Answers 2

3

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).

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, so: 1. no need to worry about single quotation marks and the unicode literals, 2. I need to figure out how to remove the comma at the end so it stops thinking the obj is a tuple?
You need to remove the comma otherwise gc_ieca is a tuple, yes. Not sure what there is to figure out, just delete the character :).
Well the issue is that I'm trying to output many of these objects and if the comma is automatically added and causes the error, then I'd need to find where in the code that outputs the JSON objects I add a comma that trips an error.
Nothing is automatically added I'm your example code -- you just make a tuple with a dict in it. If you're programmatically generating some Python code you should probably not do that though :)
2

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.

3 Comments

Do you have any advice how to address the second point you raise?
If the values for each of the name fields are just strings, as they appear, then you may just need to remove the u character. What is the intention of that character in these instances?
No intention. The character just appears when I run the script. See edit in the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.