dvdln / jsonpath-object-transform Goto Github PK
View Code? Open in Web Editor NEWTransform an object literal using JSONPath.
License: MIT License
Transform an object literal using JSONPath.
License: MIT License
Problem statement
I have data in the format
var data3 = {
ClientID:'25',
ProviderID:'26',
ProviderClientID:'DEMO1815',
SSN:'377215685'
};
Mapping data:
var template = {
foo: [{
bar:'$.ProviderClientID'
}]
};
Also tried with mapping:
var template = {
foo: ['$',{
bar:'$.ProviderClientID'
}]
};
Throws error
jsonpath-object-transform-master/node_modules/JSONPath/lib/jsonpath.js:41
var normalized = expr.replace(/['[]']/g, function($0,$1){return "[#"+(subx.push($1)-1)+"]";})
Hello,
I would like to make a suggestion:
In addition to transform the structure of the object, it would be also possible to transform the value of the attributes.
For example:
Original:
{
type: 'String'
}
Transformed:
{
type: 'text'
}
Hugs.
Just wanted to make you aware that I have reimplemented this on Python here:
https://github.com/innovationgarage/python-jsonpath-object-transform
Maybe you could link to it from the README? I've published it under the same license as your library.
Two years ago, an update was done to handle static string but this has not been published to npm, can this be published as 1.0.5.
Hi,
When i include jsonpath-object-transform module i have the following exception :
Error: Cannot find module 'lib/jsonpath-object-transform.js'
I seems that changing index.js
from : module.exports = require('lib/jsonpath-object-transform.js');
to : module.exports = require('./lib/jsonpath-object-transform.js');
fixes the issue
Regards
The JSONPath library is notoriously slow for queries. In a 5000 item array, telling it to fetch index 400 takes 5 seconds on my rather fast laptop. The "jsonpath" library is orders of magnitude faster. When I tested it to just find items matching a predicate with the same list, it took 6ms.
When using queries in templates in this library therefore suffer the same terrible performance. Having it perform a query that was selective (filtering by properties then returning only the ids of the matching items) jsonpath took 14 milliseconds while JSONPath took 37 seconds. It's not just a little bit faster but the difference is so significant that JSONPath should be shut down or fixed because it's simply not good enough for production quality code.
JSONPath has some additional syntax outside of the spec, but I don't think they are needed for most transforms. JSONPath (by the test I mentioned) is 2700x slower. Nobody should be using that library.
Hello Dvdln,
Below is my package declaration,
I am trying to use both jsonpath-object-transform and jsonpath.
{
"name": "TestRules",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"jsonpath-object-transform": "^1.0.4",
"jsonpath": "^1.0.0",
"validator": "^10.4.0"
},
"author": "",
"license": ""
}
Internally jsonpath-object-transform use "JSONPath": "^0.10.0"
Because of conflict of version in JSONPath and jsonpath, it throws an error at runtime.
After looking into the code of jsonpath-object-transform, a quick fix will be to modify the package.json
to include "jsonpath" updated version instead of JSONPath.
"dependencies": {
"jsonpath": "^1.0.0",
},
Please advice.
Regards
Gaurav
It would be nice to be able to transform items of an array using a custom callback.
Possible Syntax: (Chapter: Transform Items Returned in Array)
{ destination: ['$.path.to.sources', function(item) { return someTransformations(item); } ] }
Maybe that could also be added to transform single properties? (You would have to find a good syntax for that though).
Hi dvdLn,
I think this is a really useful package. Thanks for taking the time to write it.
When using jsonpath-object-transform V1.0.4 I have noticed that the time taken to perform
a transform is quite long. Of course this is dependent on how long my JSON is and the number of rules
to apply to it.
However, is there anything you can do to improve the speed of this library?
Are there any tips about how to improve the speed of transforms on my side, ie: particular expressions
to avoid ?
TypeError: seek[0].forEach is not a function
at seekArray (.../node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:96:15)
at walk (.../node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:52:7)
at .../node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:118:7
at Array.forEach (native)
at seekObject (.../node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:117:26)
at walk (.../node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:52:7)
at .../node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:131:5
at Object.<anonymous> (.../transform.js:47:10)
at Module._compile (module.js:435:26)
at Module._extensions..js (module.js:442:10)
Hi. Thanks for this helpful library.
Please see the following 3 code snippets. The second version fails. I am trying to apply a sub-template transformation to a filtered (via jsonpath notation) array and it fails. Without the filter, it works.
var transform = require('jsonpath-object-transform');
var o = {"cities": [
{"city": "Toronto", "population": 2.5},
{"city": "New York", "population":8.1},
{"city": "San Diego", "population": 1.6},
{"city": "Seattle", "population": 0.65}
]};
// works
var template = {
bigCities: ["$.cities[?(@.population > 2)]"]
};
var r = transform(o, template);
console.log(JSON.stringify(r, null, 4));
Results in:
{
"bigCities": [
{
"city": "Toronto",
"population": 2.5
},
{
"city": "New York",
"population": 8.1
}
]
}
var transform = require('jsonpath-object-transform');
var o = {"cities": [
{"city": "Toronto", "population": 2.5},
{"city": "New York", "population":8.1},
{"city": "San Diego", "population": 1.6},
{"city": "Seattle", "population": 0.65}
]};
// fails
var template = {
bigCities: ["$.cities[?(@.population > 2)]", {"name": "city"}]
};
var r = transform(o, template);
console.log(JSON.stringify(r, null, 4));
results in
TypeError: seek[0].forEach is not a function
at seekArray (/Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:96:15)
at walk (/Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:52:7)
at /Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:118:7
at Array.forEach (native)
at seekObject (/Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:117:26)
at walk (/Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:52:7)
at /Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:131:5
at Object.<anonymous> (/Users/dwayne/tmp/test/json-transform.js:17:9)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
var transform = require('jsonpath-object-transform');
var o = {"cities": [
{"city": "Toronto", "population": 2.5},
{"city": "New York", "population":8.1},
{"city": "San Diego", "population": 1.6},
{"city": "Seattle", "population": 0.65}
]};
// works
var template = {
bigCities: ["$.cities", {"name": "city"}]
};
var r = transform(o, template);
console.log(JSON.stringify(r, null, 4));
results in
{
"bigCities": [
{
"name": "Toronto"
},
{
"name": "New York"
},
{
"name": "San Diego"
},
{
"name": "Seattle"
}
]
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.