I have a query
CONSTRUCT { ?highValForeignTran ?hvFTPred ?hvFTObj . }
WHERE { ?highValForeignTran vocab:accounttransactions_transactionCurrency "USD" .
?highValForeignTran vocab:accounttransactions_transactionValue ?tranValue .
?highValForeignTran vocab:accounttransactions_transactionDate ?tranDate .
?highValForeignTran ?hvFTPred ?hvFTObj .
FILTER ( ?tranValue > 10000) .
FILTER ( ?tranDate >= "2013-11-23"^^xsd:date && ?tranDate <= "2013-11-23"^^xsd:date) .
}
that returns the result:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:vocab="http://localhost:2020/resource/vocab/"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:db="http://localhost:2020/resource/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:map="http://localhost:2020/resource/#">
<vocab:accounttransactions rdf:about="http://localhost:2020/resource/accounttransactions/1">
<vocab:accounttransactions_id rdf:datatype="http://www.w3.org/2001/XMLSchema#integer"
>1</vocab:accounttransactions_id>
<vocab:accounttransactions_transactionCurrency>USD</vocab:accounttransactions_transactionCurrency>
<vocab:accounttransactions_originAccountNumber>DB48939239</vocab:accounttransactions_originAccountNumber>
<vocab:accounttransactions_transactionType>Cr</vocab:accounttransactions_transactionType>
<vocab:accounttransactions_transactionDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
>2013-11-23</vocab:accounttransactions_transactionDate>
<vocab:accounttransactions_destinationAccountId rdf:resource="http://localhost:2020/resource/bankaccounts/1"/>
<vocab:accounttransactions_transactionValue rdf:datatype=
"http://www.w3.org/2001/XMLSchema#decimal">12000</vocab:accounttransactions_transactionValue>
<rdfs:label>accounttransactions #1</rdfs:label>
<vocab:accounttransactions_destinationAccountNumber>47321896544567</vocab:accounttransactions_destinationAccountNumber>
</vocab:accounttransactions>
</rdf:RDF>
when I try to parse this using Jena however I only get one triple that represents the outer accountTransactions triple:
{"http://localhost:2020/resource/accounttransactions/1":
{"subject":"http://localhost:2020/resource/accounttransactions/1",
"predicate":"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"object":"http://localhost:2020/resource/vocab/accounttransactions"}
}
I have no idea why the other triples get nested inside, but I really need to be able to parse them and send them as JSON. Here's my code:
try {
Model result = qexec.execConstruct();
JSONObject jsonShell = new JSONObject();
StmtIterator stmtIter = model.listStatements();
while ( stmtIter.hasNext() ) {
Statement stmt = stmtIter.nextStatement();
JSONObject innerJson = new JSONObject();
innerJson.put("subject", stmt.getSubject().visitWith(rdfVisitor));
innerJson.put("predicate", stmt.getPredicate().visitWith(rdfVisitor));
innerJson.put("object", stmt.getObject().visitWith(rdfVisitor));
jsonShell.put(String.valueOf(stmt.getSubject().visitWith(rdfVisitor)), innerJson);
}
System.out.println(resultJson.toString());
}
finally {
qexec.close();
}
RDFVisitor rdfVisitor = new RDFVisitor() {
@Override
public Object visitURI(Resource r, String uri) {
return uri;
}
@Override
public Object visitLiteral(Literal l) {
return l.getLexicalForm();
}
@Override
public Object visitBlank(Resource r, AnonId id) {
return id.getLabelString();
}
};
I wonderered if Statement.getProperty()
might do the trick, but couldn't discover a way to create a Property
instance.
jsonShell.put("X", innerJson);
add a bunch of "X" elements to the JSON, or keep overwriting the previous one? All your triples in this model have the same subject, so if you're doingjsonShell.put(<subject-uri>,innerJson)
and if that overwrites an existing entry, you'll only end up with one.