2

I am trying to save this json object:

[
{
    "patient_id": "59b70b0e-51cb-4215-b65a-db470067c8de"
},
[
    {
        "tel_1_preferred_p": true,
        "adr_region": "OK",
        "tel_2_number": "800-979-6786",
        "__modelname__": "Demographics",
        "adr_city": "Bixby",
        "ethnicity": null,
        "adr_postalcode": "74008",
        "name_family": "John",
        "name_middle": null,
        "tel_1_type": "h",
        "tel_2_type": "c",
        "name_prefix": null,
        "email": "[email protected]",
        "name_given": "Smith",
        "adr_street": "23 Church Rd",
        "bday": "1965-08-09",
        "__documentid__": "65cd1101-b047-4ce5-afc0-778d033229ca",
        "tel_1_number": "800-870-3011",
        "preferred_language": "EN",
        "gender": "male",
        "name_suffix": null,
        "tel_2_preferred_p": true,
        "race": null,
        "adr_country": "USA"
    }
],
[
    {
        "startDate": "2014-10-02T00:00:00Z",
        "name_identifier": "195967001",
        "name_system": null,
        "notes": "None",
        "name_title": "Asthma",
        "__modelname__": "Problem",
        "endDate": "2014-10-13T00:00:00Z",
        "__documentid__": "eaba4e92-3aaf-4d8c-a7db-8cfd563290c6"
    },
    {
        "startDate": "2014-12-02T00:00:00Z",
        "name_identifier": "161155000",
        "name_system": null,
        "notes": "None",
        "name_title": "School problem (finding)",
        "__modelname__": "Problem",
        "endDate": "2014-12-14T00:00:00Z",
        "__documentid__": "3058b12d-434d-48da-8b14-05dd485946cb"
    },
    {
        "startDate": "2008-07-29T00:00:00Z",
        "name_identifier": "185903001",
        "name_system": "http://purl.bioontology.org/ontology/SNOMEDCT/",
        "notes": null,
        "name_title": "Needs influenza immunization",
        "__modelname__": "Problem",
        "endDate": "2010-09-13T00:00:00Z",
        "__documentid__": "d982c117-43c3-407f-bd9e-27dc59008938"
    }
],
[
    {
        "name": "Amputation of the foot (procedure)",
        "comments": null,
        "provider_institution": null,
        "date_performed": "2014-10-22T00:00:00Z",
        "name_value": "None",
        "__modelname__": "Procedure",
        "__documentid__": "221df5bd-16a8-4763-9827-06fb305b91e5",
        "provider_name": null,
        "name_abbrev": "180030006",
        "name_type": "http://purl.bioontology.org/ontology/SNOMEDCT/",
        "location": null
    }
]

]

to an XML file or RDF in python code. I didn't find posts about rdf so I tried for XML. I tried also to change the list to dict like in this post:

Python Json loads() returning string instead of dictionary?

but it is not working. In views.py I have:

 fileroot = '[{"patient_id":"'+record_id+'"},'+content0+','+content1+','+content2+']'
jsonData = simplejson.loads(fileroot)

It returns 'list' object has no attribute 'items'. And if I change it to :

  fileroot = '[{"patient_id":"'+record_id+'"},'+content0+','+content1+','+content2+']'
jsonData = simplejson.loads(fileroot)[0]

It returns nothing. I also tried from this link:

https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch06s05.html

but it is not working again.

2
  • What's your problem? Your json file works in my console. Commented Mar 13, 2015 at 11:14
  • I can't transform it to XML or RDF and save it Commented Mar 13, 2015 at 12:17

1 Answer 1

1

This will tranlsate your json object to xml

import simplejson
content = simplejson.loads(YourJsonObject)


from xml.etree.ElementTree import Element
from xml.etree.ElementTree import tostring

def dict_to_xml(tag, d):
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element(tag)
    for key, val in d.items():
        child = Element(key)
        child.text = str(val)
        elem.append(child)
    return elem

for i in range(1,len(content)):
    e = dict_to_xml('SomeNode',content[i][0])

    # print e
    print tostring(e)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.