Pages

Wednesday, December 29, 2010

Converting JSON to RDF/XML with Groovy

Mark's new CCO/RDF hosting functionality (see also my post two days ago) requires RDF/XML format, so I updated my code to convert the Chempedia Substances data into RDF/XML instead of N3 (I have asked Rich to put a new download link online). This is the Groovy code I used:

import groovy.xml.MarkupBuilder
import groovy.util.IndentPrinter

input = new File("substances.json")
json = new JsonSlurper().parse(input);

def writer = new StringWriter()
def xml = new MarkupBuilder(
  new IndentPrinter(new PrintWriter(writer))
)
xml.'rdf:RDF'(
  'xmlns:rdf':
    'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  'xmlns:dc' :
    'http://purl.org/dc/elements/1.1/',
  'xmlns:iupac' :
    'http://www.iupac.org/',
  'xmlns:cp' :
    'http://rdf.openmolecules.net/chempedia/onto#',
  'xmlns:owl' :
    'http://www.w3.org/2002/07/owl#'
) {
  json.each { substance ->
    xml.'rdf:Description'(
      'rdf:about': substance.uri
    ) {
      xml.'dc:identifier'(substance.gsid)
      xml.'owl:sameAs'(
        'rdf:resource' :
        'http://rdf.openmolecules.net/?' +
        substance.inchi
      )
      xml.'iupac:inchi'(
        'http://rdf.openmolecules.net/?' +
        substance.inchi
      )
      for (int i = 0; i<substance.namings.size(); i++)
      {
        naming = substance.namings.get(i);
        namingURI = substance.uri + "/naming" + i;
        xml.'cp:hasNaming' {
          xml.'rdf:Description' {
            xml.'cp:hasName'(naming.name)
            xml.'cp:hasStatus'(naming.status)
            xml.'cp:hasScore'(naming.score)
          }
        }
      }
    }
  }
}
println writer.toString();