diff --git a/README.md b/README.md index 223c70d..bc1dc27 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![npm](https://badge.fury.io/js/jsonpath-object-transform.png)](http://badge.fury.io/js/jsonpath-object-transform) -Pulls data from an object literal using JSONPath and generate a new objects based on a template. Each of the template's properties can pull a single property from the source data or an array of all results found by its JSONPath. When pulling an array of data you can also supply a subtemplate to transform each item in the array. +Pulls data from an object literal using JSONPath and generate a new object based on a template. Each of the template's properties can pull a single property from the source data or an array of all results found by its JSONPath. When pulling an array of data you can also supply a subtemplate to transform each item in the array. Both keys and values will be interpolated. JSONPath is like XPath for JavaScript objects. To learn the syntax, read the documentation for the [JSONPath](https://www.npmjs.org/package/JSONPath) package on npm and the [original article](http://goessner.net/articles/JsonPath/) by Stefan Goessner. @@ -115,7 +115,7 @@ Use an `Array` with a `String` and an `Object` to assign all results returned fr #### Example ```js var template = { - foo: [$..example, { + foo: ['$..example', { bar: '$.demo' }] }; @@ -142,3 +142,98 @@ Result: ] } ``` + +### Merge Items Returned in Array into a single object +```js +{ destination: ['$.path.to.sources', { '$.item.key': '$.item.path' }, {merge: true}] } +``` +Use an `Array` with a `String` and an `Object` to assign all results returned from your JSONPath and transform each of the objects with a subtemplate. The "merge: true" option will merge the results into one object. e.g. +```js +[{a:'b'},{c:'d'},{e:'f'}] +``` +-> +```js +{a:'b',c:'d',e:'f'} +``` + +#### Example +```js +var template = { + foo: ['$..example', { + '$.key': '$.demo' + }, {merge: true}] +}; + +var data = { + a: { + example: { + key: 'a', + demo: 'baz' + } + }, + b: { + // NOTE: you can use arrays or objects in this example and the previous one + example: [{ + key: 'b', + demo: 'qux' + },{ + key: 'c', + demo: 'max' + }] + } +}; +``` +Result: +```js +{ + foo: { + a: 'baz', + b: 'qux', + c: 'max' + } +} +``` + +### Dynamic Keys +```js +{ '$.aKey': ['$.path.to.sources', { '$.item.key': '$.item.path' }] } +``` +Use any valid JSON path as the key to assign a dynamic key to your result object + +#### Example +```js +var template = { + '$.a.example.demo': ['$..example', { + '$.key': '$.demo' + }, {merge: true}] +}; + +var data = { + a: { + example: { + key: 'a', + demo: 'baz' + } + }, + b: { + // NOTE: you can use arrays or objects in this example and the previous one + example: [{ + key: 'b', + demo: 'qux' + },{ + key: 'c', + demo: 'max' + }] + } +}; +``` +Result: +```js +{ + "baz": { + "a": "baz", + "b": "qux", + "c": "max" + } +} +``` diff --git a/lib/jsonpath-object-transform.js b/lib/jsonpath-object-transform.js index 022db64..329af0b 100644 --- a/lib/jsonpath-object-transform.js +++ b/lib/jsonpath-object-transform.js @@ -33,7 +33,11 @@ */ function walk(data, path, result, key) { var fn; - + // if the key starts with $., assume that it's dynamic and should be evaluated + if (key && key.toString().indexOf('$.')>-1) { + key = jsonPath.eval(data, key); + } + switch (type(path)) { case 'string': fn = seekSingle; @@ -46,6 +50,8 @@ case 'object': fn = seekObject; break; + default: + result[key] = path; } if (fn) { @@ -73,10 +79,9 @@ */ function seekSingle(data, pathStr, result, key) { if(pathStr.indexOf('$') < 0){ - result[key] = pathStr; - }else{ - var seek = jsonPath.eval(data, pathStr) || []; - + result[key] = pathStr; + } else { + var seek = jsonPath.eval(data, pathStr) || []; result[key] = seek.length ? seek[0] : undefined; } } @@ -93,13 +98,26 @@ var subpath = pathArr[1]; var path = pathArr[0]; var seek = jsonPath.eval(data, path) || []; + var mergeArray = pathArr[2] && pathArr[2].merge; if (seek.length && subpath) { - result = result[key] = []; - - seek[0].forEach(function(item, index) { - walk(item, subpath, result, index); + result[key] = []; + seek.forEach(function(items, seekIndex) { + // if it's just a regular object, make it into an array of one so we can still iterate it + if(!Array.isArray(items)) { + items = [items]; + } + items.forEach((item, itemIndex) => { + // result[key].length is to push each new item to the array + walk(item, subpath, result[key], result[key].length); + }); }); + if(mergeArray) { + // merge the individual objects in the array into one big object if the merge option is set to true + result[key] = result[key].reduce((reduced, el) => { + return Object.assign(reduced, el); + }, {}); + } } else { result[key] = seek; } @@ -130,10 +148,31 @@ * @returns {object} */ return function(data, path) { - var result = {}; - + var result = {}, + needsWrapper = Array.isArray(data) && Array.isArray(path); + // wrap the data and path in a temp variable that will serve as the key for our initial iteration + // this is to resolve the fact that the code doesn't handle root level arrays natively + if (needsWrapper) { + data = { temp: data }; + path = { temp: path }; + } + walk(data, path, result); + // unwrap the data before returning it + if (needsWrapper) { + result = result.temp; + } + const undefResult = result['undefined']; + /** + * allow direct access to values. + * @example template = "$.bob" and element = {bob:123} + * without this, we return {undefined: 123}, with this, we return 123 + * if nothing is found, this allows us to return undefined + */ + if (result.hasOwnProperty('undefined')) { + result = undefResult; + } return result; };