How to create a new valid object and stuff into existing doc?

34 views
Skip to first unread message

Eeee

unread,
Jan 24, 2018, 7:33:32 PM1/24/18
to jsonschema - An implementation of JSON Schema for Python
Hi - I apologize if I'm barking up the wrong tree here, but I seem to be missing something:

I have defined a schema doc, snippet below, allowing for a null plateId in the json doc:

{
   
"$schema": "http://json-schema.org/draft-04/schema#",
   
"definitions": {
       
"PlateTracking": {
           
"type": "object",
           
"properties": {
               
"accessionNumber": {
                   
"type": "string"
               
},
               
"barcode": {
                   
"type": "string"
               
},
           
},
       
},
       
...
       
"Plate": {
           
"title": "Plate",
           
"type": "object",
           
"properties": {
               
"plateId": {
                   
"oneOf": [
                       
{
                           
"type": "null"
                       
},
                       
{
                           
"$ref": "#/definitions/PlateTracking"
                       
}
                   
]
               
},
               
"name": {
                   
"type": "string"
               
},
               
...


Example of my valid json is with a null plateId object:

{
   
"plate": {
       
"plateId": null,
       
"name": "foobar",
       
...



I have a function that successfully validates the json data doc against the jsonschema doc:

def validate_json(schema_file_name, data_file_name, read_write_mode = 'r'):
   
# load the schema file as read-only
   
with open(schema_file_name, 'r') as f:
        schema_data
= f.read()
       
    schema
= json.loads(schema_data)
   
   
# load the data file
   
with open(data_file_name, read_write_mode) as f:
        data
= f.read()
       
    json_data
= json.loads(data)
   
   
# validate
   
try:
        jsonschema
.validate(json_data, schema)
       
return json_data
   
except jsonschema.ValidationError as e:
       
print(e.message)
       
raise e
   
except jsonschema.SchemaError as e:
       
print(e)
       
raise e


   
return None




My issue is this: given the schema object, how do I create a PlateTracking object and attach it to plate.plateId?

I am not looking to create a json string, but am interested in dealing with Python classes/objects.  Is this possible?

Again, apologies, if missing something very basic here.  Hoping so.

Best,
E

Tiago Ferraz

unread,
Jan 25, 2018, 8:00:06 AM1/25/18
to jsonschema - An implementation of JSON Schema for Python
Hi E.

What I understood is that a Plate may have a plate tracking. Being these 2 different objects and you want to create one JSON schema with both, it seems you want something similar to https://spacetelescope.github.io/understanding-json-schema/structuring.html#structuring, which is what you already do using "$ref": "#/definitions/PlateTracking". So , what exactly you want to do?

Best,
Tiago
Message has been deleted

Eeee

unread,
Jan 25, 2018, 2:41:18 PM1/25/18
to jsonschema - An implementation of JSON Schema for Python
Thanks for asking, Tiago.  Yes, your understand is exactly right.  I guess my issue is the following:
I know I can manually create the plate tracking via the dictionary - something like:

   track = {}
   track
['accessionNumber'] = 'foo'
   track
['barcode'] = '123456789'
   plate
['plateId'] = track
   
...



but the above would require intimate knowledge of the attribute names, etc..
I was hoping that with the schema, (kinda like you get when you transform an XSD into classes), I'd be able to have Python classes (or an object model created dynamically) to do something like the following:



   plate
.plateId = PlateTracking('foo', '123456789')





Maybe that's not possible with this?

Thanks again!

Tiago Ferraz

unread,
Jan 26, 2018, 7:41:24 AM1/26/18
to jsonschema - An implementation of JSON Schema for Python
I understand, 

JSON maps well for Python lists and dictionaries, but for objects I guess you need to create a constructor that would do that for you in class Plate. I see this like a ORM, but for JSON, so probably there is a library for doing this. Or easier, I think this is what you want to do https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch06s02.html and http://www.yilmazhuseyin.com/blog/dev/advanced_json_manipulation_with_python/
Reply all
Reply to author
Forward
0 new messages