我想将JSON(不是JSON-LD)数组转换为Java中的RDF。我在论坛上看到过类似的帖子,但没有确切的答案。 JSON数组包含对象和数组,例如:
{
"results": [
{
"record_id": "3d87f4df-f17e-4632-9449",
"demographics": { "gender":"female", "race":"", "age":20 }
},
{
"record_id": "ec5ca92d-865a-431f-9984",
"demographics": { "gender":"male", "age":118 }
},
{
"record_id": "0a79ecf0-83d8-4148-9054",
"demographics": { "gender":"female", "age":118 }
},
{
"record_id": "229276f8-1893-480b-b6e7",
"demographics": { "gender":"female", "age":35 }
},
{
"record_id": "0574cc3b-fb9c-495f-851c",
"demographics": { "gender":"female", "age":40 }
},
{
"record_id": "f3ccfdf6-231e-4a3e-bee0",
"demographics": { "gender":"male", "age":118 }
}
]
}
有任何想法吗?
谢谢!
最佳答案
我建议您不要对转换进行硬编码。
对于RDBMS源(顺便说一句,为什么不使用它们),有W3C标准化的R2RML(r2rml)。
对于JSON和XML源,有RML,它是R2RML的非官方扩展。
RML Mapper是RML的Java实现:$ bin/RML-Mapper -m ~/Desktop/mappings.ttl -o ~/Desktop/results.ttl
通常,应将rml:iterator "$.results.[*]"
放在rml:logicalSource
部分中以遍历数组元素。确切的答案取决于您要使用哪些词汇表以及要实现的数据模型。
假设您需要这样的东西:
@prefix exr: <http://example.org/resource/> .
@prefix exo: <http://example.org/ontology/> .
exr:gender_female
a exo:Gender ;
rdfs:label "female" .
exr:gender_male
a exo:Gender ;
rdfs:label "male" .
exr:record_3d87f4df-f17e-4632-9449
a exo:Record ;
exo:patient_age 20 ;
exo:patient_gender exo:gender_female .
exr:record_ec5ca92d-865a-431f-9984
a exo:Record ;
exo:patient_age 118 ;
exo:patient_gender exo:gender_male .
然后,您的映射应为:
@prefix rr: <http://www.w3.org/ns/r2rml#>.
@prefix rml: <http://semweb.mmlab.be/ns/rml#>.
@prefix ql: <http://semweb.mmlab.be/ns/ql#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix exo: <http://example.org/ontology/>.
@prefix exr: <http://example.org/resource/>.
<#RecordMapping>
rml:logicalSource [
rml:source "/home/skralin/Desktop/results.json";
rml:referenceFormulation ql:JSONPath;
rml:iterator "$.results.[*]"
];
rr:subjectMap [
rr:template "http://example.org/resource/record_{record_id}";
rr:class exo:Record
];
rr:predicateObjectMap [
rr:predicate exo:patient_gender;
rr:objectMap [
rr:parentTriplesMap <#GenderMapping>
]
];
rr:predicateObjectMap [
rr:predicate exo:patient_age;
rr:objectMap [
rml:reference "demographics.age" ;
rr:datatype xsd:integer
]
].
<#GenderMapping>
rml:logicalSource [
rml:source "/home/skralin/Desktop/results.json";
rml:referenceFormulation ql:JSONPath;
rml:iterator "$.results.[*].demographics.gender"
];
rr:subjectMap [
rr:template "http://example.org/resource/gender_{$}";
rr:class exo:Gender
];
rr:predicateObjectMap [
rr:predicate rdfs:label;
rr:objectMap [
rml:reference "$"
]
].