Skip to content

Commit

Permalink
Merge pull request #1 from comake/fix/json-arrays
Browse files Browse the repository at this point in the history
Fix: Allow json arrays
  • Loading branch information
adlerfaulkner authored Jul 6, 2022
2 parents d1304e8 + 5686a84 commit bf7a5bd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/input-parser/JSONParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ class JsonParser {
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
.map((e) => {
if (Array.isArray(e)) {
return e.map((eItem) => eItem.toString());
}
return e.toString();
}); // return only strings
}
}

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 bf7a5bd

Please sign in to comment.