Skip to content

Commit

Permalink
Merge pull request #33 from comake/fix/json-arrays
Browse files Browse the repository at this point in the history
Allow array values in json
  • Loading branch information
ThibaultGerrier authored Jul 7, 2022
2 parents d1304e8 + b339992 commit e57e5d6
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/input-parser/JSONParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ class JsonParser {
getData(index, selector) {
const sel = selector.replace(/^PATH~/, '');
const splitter = sel.startsWith('[') ? '' : '.';
return JSONPath({
const arr = JSONPath({
path: `${this.paths[index]}${splitter}${sel}`,
json: this.json,
resultType: selector.startsWith('PATH~') ? 'pointer' : 'value',
})
.filter((e) => e !== null && e !== undefined) // null values are ignored (undefined shouldn't happens since input is json)
.map((e) => e.toString()); // return only strings
.filter((e) => e !== null && e !== undefined); // null values are ignored (undefined shouldn't happens since input is json)

if (arr.length === 1 && Array.isArray(arr[0])) {
return arr[0].map((e) => e.toString());
}
return arr.map((e) => e.toString());
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/arrayValueMapping/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name":"Tom A.",
"favorite-numbers": [ 3, 33, 13 ]
}
33 changes: 33 additions & 0 deletions tests/arrayValueMapping/mapping.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix schema: <http://schema.org/> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@base <http://sti2.at/> . #the base for the classes

<#LOGICALSOURCE>
rml:source "./tests/arrayValueMapping/input.json";
rml:referenceFormulation ql:JSONPath;
rml:iterator "$".


<#Mapping>
rml:logicalSource <#LOGICALSOURCE>;

rr:subjectMap [
rr:termType rr:BlankNode;
rr:class schema:Person;
];


rr:predicateObjectMap [
rr:predicateMap [ rr:constant "http://schema.org/name" ];
rr:objectMap [ rml:reference "name" ];
];

rr:predicateObjectMap [
rr:predicateMap [ rr:constant "http://example.com/favorite-numbers" ];
rr:objectMap [
rml:reference "favorite-numbers" ;
rr:datatype "http://www.w3.org/2001/XMLSchema#integer" ;
];
] .
21 changes: 21 additions & 0 deletions tests/arrayValueMapping/out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"@type": "http://schema.org/Person",
"http://schema.org/name": "Tom A.",
"http://example.com/favorite-numbers": [
{
"@value": "3",
"@type": "http://www.w3.org/2001/XMLSchema#integer"
},
{
"@type": "http://www.w3.org/2001/XMLSchema#integer",
"@value": "33"
},
{
"@type": "http://www.w3.org/2001/XMLSchema#integer",
"@value": "13"
}
],
"@id": "_:http%3A%2F%2Fsti2.at%2F%23Mapping_1"
}
]
16 changes: 16 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ it('Basic straight double mapping', async () => {
assert.equal(result.length, 2);
});

it('Array Value mapping', async () => {
const options = {};
let result = await parser.parseFile('./tests/arrayValueMapping/mapping.ttl', './tests/arrayValueMapping/out.json', options).catch((err) => { console.log(err); });
result = helper.cutArray(result);
assert.equal(result['http://schema.org/name'], 'Tom A.');
assert.equal(result['http://example.com/favorite-numbers'].length, 3);
assert.equal(result['http://example.com/favorite-numbers'][0]['@value'], '3');
assert.equal(result['http://example.com/favorite-numbers'][0]['@type'], 'http://www.w3.org/2001/XMLSchema#integer');
assert.equal(result['http://example.com/favorite-numbers'][1]['@value'], '33');
assert.equal(result['http://example.com/favorite-numbers'][1]['@type'], 'http://www.w3.org/2001/XMLSchema#integer');
assert.equal(result['http://example.com/favorite-numbers'][2]['@value'], '13');
assert.equal(result['http://example.com/favorite-numbers'][2]['@type'], 'http://www.w3.org/2001/XMLSchema#integer');
assert.equal(result['@type'], 'http://schema.org/Person');
assert.equal(Object.keys(result).length, 4);
});

it('Live mapping', async () => {
const options = {
// baseMapping:["http://sti2.at/#SPORTSmapping"],
Expand Down

0 comments on commit e57e5d6

Please sign in to comment.