@mnakhla/rocketrml
Version:
This is a forked rocketrml mapper for the RDF mapping language
36 lines (30 loc) • 1.05 kB
JavaScript
const { JSONPath } = require('jsonpath-plus');
const helper = require('./helper');
class JsonParser {
constructor(inputPath, iterator, options) {
this.iterator = iterator;
this.json = helper.readFileJSON(inputPath, options);
this.paths = JSONPath({
path: iterator,
json: this.json,
resultType: 'path',
});
}
getCount() {
return this.paths.length;
}
getData(index, selector) {
const sel = selector.replace(/^PATH~/, '');
const splitter = sel.startsWith('[') ? '' : '.';
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)
if (arr.length === 1 && Array.isArray(arr[0])) {
return arr[0].map((e) => e.toString());
}
return arr.map((e) => e.toString());
}
}
module.exports = JsonParser;