jsonpath-plus / jsonpath Goto Github PK
View Code? Open in Web Editor NEWA fork of JSONPath from http://goessner.net/articles/JsonPath/
License: Other
A fork of JSONPath from http://goessner.net/articles/JsonPath/
License: Other
Given the following JSON:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
]
}
}
JSONPath returns the same result for the following 2 queries:
JSONPath({data: data, path: "$.store.book", wrap: true});
JSONPath({data: data, path: "$.store.book", wrap: false});
[
[
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
]
]
I believe for wrap: false, the outer array should be removed like this:
[
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
]
It would be great to expand the context in which the comma could be used (e.g., between filtering expressions to allow simultaneous matching of both an array filter and property filter as $.book[(@.length - 1), ?(@.prop == 'abc')]
as well as at a higher level such as $.book,$.car
) so as to better serve as an OR operator.
An implementation supporting this will probably require replacing the regexes with a genuine parser.
Hi,
Given a structure like this:
[
{
"id": 1,
"tags": ["foo","bar"]
},
{
"id": 2,
"tags": ["bar","zot"]
}
]
How can I do a selection that selects the whole object if the tag array contains "foo"?
This way I only get the tags-arrays back:
'$..tags[?(@ === "foo")]'
My guess was:
'$..[?(@.tags[?(@property === "foo")])]'
But that gives me
Error in the given query: Lexical error on line 1. Unrecognized text.
...perty === "foo")])]
-----------------------^
This is what I want back from the query:
[
{
"id": 1,
"tags": ["foo","bar"]
}
]
Any help would be greatly appreciated.
I was wondering whether the library also supports compound expressions such as "status='ok' and count>7"?
The commit 3d1a14a (in master
) allows one to add a parser function to the sandbox for a more explicit inclusion (though it doesn't avoid an eval
with access to global variables). I think should become the required approach once we may move to evaluating in a true sandbox, using something perhaps like this: http://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/ .
I would also like to see inclusion of something like PR #4 to avoid the need for eval()
in those cases where filters are used but where arbitrary JavaScript is not needed. I also think this ought to become the new default for security reasons.
Note that the new preventEval
option prevents use of filters entirely, avoiding the main concern, but also (unduly) restricting what features can be used.
TypeError: Object doesn't support this property or method
Occurs on this line:
var ret = this.evaluate((objArgs ? opts.json : obj), (objArgs ? opts.path : expr));
The json string is '$.questions[?(@.name=='questioame')]
We are having problems using JSONPath and angular-mock at the same time in Karma. After some debugging we found out that angular-mock creates a window.module variable.
This causes JSONPath to not create the window.JSONPath function.
From the code:
if (typeof module === 'undefined') {
window.jsonPath = { // Deprecated
eval: JSONPath.eval
};
window.JSONPath = JSONPath;
}
else {
module.exports = JSONPath;
}
The only way around now is to make sure that JSONPath loads before the angular-mock file.
A few expressions are not immediately clear on whether they are supported. Desire to add tests and documentation for the likes of the following: $.
, $[0]
, $.[0]
, or $.['prop']
.
I'm trying to install Ghost on FreeBSD and it seems like it depends on the JSONPath package that fails to install:
# npm install --production
npm WARN cannot run in wd [email protected] npm install semver && node -e "require('./core/server/utils/startup-check.js').nodeVersion()" (wd=/usr/home/ghost/ghost)
/
> [email protected] postinstall /usr/home/ghost/ghost/node_modules/jsonpath
> node lib/aesprim.js > generated/aesprim-browser.js
module.js:340
throw err;
^
Error: Cannot find module '/usr/home/ghost/ghost/node_modules/jsonpath/lib/aesprim.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3
npm ERR! FreeBSD 10.3-RELEASE
npm ERR! argv "node" "/usr/local/bin/npm" "install" "--production"
npm ERR! node v0.10.40
npm ERR! npm v2.14.7
npm ERR! code ELIFECYCLE
npm ERR! [email protected] postinstall: `node lib/aesprim.js > generated/aesprim-browser.js`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the [email protected] postinstall script 'node lib/aesprim.js > generated/aesprim-browser.js'.
npm ERR! This is most likely a problem with the jsonpath package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node lib/aesprim.js > generated/aesprim-browser.js
npm ERR! You can get their info via:
npm ERR! npm owner ls jsonpath
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /usr/home/ghost/ghost/npm-debug.log
The relevant lines (as I thought) from nom-debug.log
as follows:
2023 info install [email protected]
2024 info postinstall [email protected]
2025 verbose unsafe-perm in lifecycle false
2026 info [email protected] Failed to exec postinstall script
2027 verbose unlock done using /root/.npm/_locks/jsonpath-b4d7f2175e23260d.lock for /usr/home/ghost/ghost/node_modules/jsonpath
2028 verbose stack Error: [email protected] postinstall: `node lib/aesprim.js > generated/aesprim-browser.js`
2028 verbose stack Exit status 8
2028 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:214:16)
2028 verbose stack at EventEmitter.emit (events.js:98:17)
2028 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:24:14)
2028 verbose stack at ChildProcess.emit (events.js:98:17)
2028 verbose stack at maybeClose (child_process.js:766:16)
2028 verbose stack at Process.ChildProcess._handle.onexit (child_process.js:833:5)
2029 verbose pkgid [email protected]
2030 verbose cwd /usr/home/ghost/ghost
2031 error FreeBSD 10.3-RELEASE
2032 error argv "node" "/usr/local/bin/npm" "install" "--production"
2033 error node v0.10.40
2034 error npm v2.14.7
2035 error code ELIFECYCLE
2036 error [email protected] postinstall: `node lib/aesprim.js > generated/aesprim-browser.js`
2036 error Exit status 8
2037 error Failed at the [email protected] postinstall script 'node lib/aesprim.js > generated/aesprim-browser.js'.
2037 error This is most likely a problem with the jsonpath package,
2037 error not with npm itself.
2037 error Tell the author that this fails on your system:
2037 error node lib/aesprim.js > generated/aesprim-browser.js
2037 error You can get their info via:
2037 error npm owner ls jsonpath
2037 error There is likely additional logging output above.
2038 verbose exit [ 1, true ]
2039 verbose unbuild node_modules/jsonpath
2040 info preuninstall [email protected]
2041 info uninstall [email protected]
2042 verbose unbuild rmStuff [email protected] from /usr/home/ghost/ghost/node_modules
2043 info postuninstall [email protected]
2044 silly gentlyRm /usr/home/ghost/ghost/node_modules/jsonpath is being purged from base /usr/home/ghost/ghost
2045 verbose gentlyRm don't care about contents; nuking /usr/home/ghost/ghost/node_modules/jsonpath
2046 silly vacuum-fs purging /usr/home/ghost/ghost/node_modules/jsonpath
2047 silly vacuum-fs quitting because other entries in /usr/home/ghost/ghost/node_modules
I'm not sure what needs to be done but am more than happy to help debugging. Any help would be appreciated.
Hi,
Is it possible to have an optional feature to return the matching elements of an document, with reference to it and not as a copy. This will help to modify the original document if required through the results itself.
Thanks.
Given the potential for ambiguity (e.g., with a property named $
), I think it is better to require the form $.aProperty
instead of allowing the bare form aProperty
.
Dear All
please help bellow example.
var masterdata = {
"seedData": {
"eligibleChargeTypes": {
"eligibleChargeType": [
{
"code": "Activation",
"name": "Activation",
"resource": {
"code": "Both",
"name": "Both"
}
},
{
"code": "ProductChange",
"name": "Product Change"
}
]
}
}
}
Expected out put :"code": "Activation"
if resource.code =''both' is sucess then i need the parent one data.
output= "";
output += jsonPath(masterdata, "$.seedData.eligibleChargeTypes.eligibleChargeType[*].resource[?(@.code=='Both')]..code").toJSONString() + "\n>";
document.write(output);
const originalPath = "$['foo']['bar']";
jsonPath({
json: { foo: { bar: 'baz' } },
path: originalPath,
wrap: false,
resultType: 'value'
});
const path = jsonPath.toPathString(jsonPath.toPathArray(originalPath));
Expected: path === originalPath
Actually: path !== originalPath && path === "$['bar']"
would it be possible do decouple the part that finds the matching objects from the part that traverses the tree?
over here at Floby/node-json-streams#4
we're working on a streaming parser and need something to select the parts of the json object once they are discovered.
this basically involves a traversal, but it is asyncronous.
what input do you need to make JSONPath work?
is a root object, a path, and a current object enough?
wrap
has been set to true
by default which can produce ambiguous results if the result could be either an empty array or a failure to find anything. For version 1.0, we should make this false
by default.
I spend a while to go through the document and code to figure this out.
An expression like $..book[?(@.title='"Sword of Honour"')]
will not work.
It should be something like $..book[?(@.title=='"Sword of Honour"')]
or $..book[?(@.title==='"Sword of Honour"')]
like in JavaScript. Not sure if this is obvious, but I suggest to add it to the document since it is different from XPath syntax. Thanks.
This is my object, can query for the whole object but exclude the template
field?
{
sections:
[
{
name: "part1",
title: "Part I",
children: [{
name: "1",
title: "part1.1",
template:"part1.1 template"
}, {
name: "2",
title: "part1.2",
template:"part1.2 template"
}]
},
{
name:"part2",
//...
}
]
}
In the browser below IE9-, it have a problem.
the browser bleow IE9- not support the function 'Array.isArray'.
While it may be tempting to rely on another library to resolve JSON References, I believe it will be suitable for JSONPath to optionally and recursively resolve JSON references (as well as optionally cache the resulting tree), depending on whether the user's path traverses any objects indicating JSON references for resolution.
For the demo file, added for sake of quick testing, would be nice to add form controls to adjust the arguments and show different result types, with color-formatting on the JS, JSON results, and path expression textbox.
remark
and remark-lint
changed a bunch since it was implemented here. Most notably, see the [email protected]
changelog, and the releases for remark
page to find out more.
Or, feel free to ping us on Gitter or me below!
Cheers ๐
Titus
var sample1 = {
"book" : {
"name" : "book 1",
"type" : "type 1"
}
}
var sample2 = {
"book" : [{
"name" : "book 1",
"type" : "type 1"
}, {
"name" : "book 2",
"type" : "type 2"
}, {
"name" : "book 3",
"type" : "type 3"
}
]
}
Can any one help me to construct a single JSON path to extract the book name for which the type = "type 1" from both the samples? The problem is the book can be an array or an object but I can have only one json path to extract the value. Any help would be appreciated.
=~
- left matches regular expression [?(@.name =~ /foo.*?/i)]
This will allow JSONPath to be trivially imported into web workers, without breaking compatibility in normal scenarios.
See https://developer.mozilla.org/en-US/docs/Web/API/Window/self for details.
I'm a noob user, trying to understand, could i create tests on my api with nodejs + json path.
My common tasks would be to fill up empty jsons with data, but using paths (i need to maintain actively growing and changing API service, that's why i cannot duplicate json object in tests, i need to load it from prepared file with json text, downloaded from dev git repository, this json file is a latest representation of a request considering the latest specification). If I won't do this, I will need to change all tests every time when api would change inside of the development process.
In Ruby implementation of jsonpath I can get access to the object right when i'm executing json path and change a value of finded node on the fly.
How could this be possible in node?
I found essential to use node js to test _json_ API, but for now it seems that ruby community is more prepared for that... or am I wrong?
Please forgive the title. I don't think it is a great one but I had difficulty coming up with an appropriate one for the bug report.
Given the source JSON, using $.store.book[*].author
is meant to return each author value. In my testing, this worked, but I noticed my callback was called 4 times as opposed to being called once with an array of four items. This seems odd as $.store.book will call the callback once and send everything.
I'm working on a generic viewer for JSONPath and because of this, I don't know when the work is done - if that makes sense.
Is this expected?
$ npm install JSONPath
[email protected] node_modules/JSONPath
Isn't it 0.11.0?
BTW,
https://www.npmjs.com/package/JSONPath
says 404.
var data = {
grouped_people: {
'friends': [
{name: 'Steve', country: ['NZ','US']},
{name: 'Daniel', country: ['NZ']},
{name: 'Bob', country: ['US']}
],
'enemies': [
{name: 'Evil Steve', country: ['NZ','US']}
]
}
}
i am trying to get people who have country as "US", which should result in getting me the records of Steve,Bob, and Evil Steve.
I tried this below query but it gets me only the Bob.
var values= JSONPath({json: data, path: "$..[?(@.country=='US')]"});
Is there anything i could use to specify "in"/"has" operator for an array?
some thing like below.
var values= JSONPath({json: data, path: "$..[?('US' in @.country)]"});
Example:
var data = {
"datafield": [
{ "tag": "035", "subfield": {"@code": "a", "#text": "1879" } },
{ "@tag": "042", "subfield": {"@code": "a", "#text": "5555" } } ] }
//this works
jsonPath.eval( data, "$.datafield[?(@.tag=='035')]");
//but this doesn't
jsonPath.eval( data, "$.datafield[?(@.@tag=='035')]");
Have object:
var obj = {
"test1": {
"test2": {
"test3.test4.test5": {
"test7": "value"
}
}
}
}
Doing request:
jsonPath.eval(obj, "test1.test2['test3.test4.test5']")
or
jsonPath.eval(obj, 'test1.test2["test3.test4.test5"]')
in both cases returns:
[]
Expectation is:
{
"test7": "value"
}
when i have a data structure like
{
city1: ['BJ'],
city2: 'BJ'
}
when i use jsonpath to get the json value,
$.city1 it will return ['BJ']
$.city2 it will return ['BJ'] as well,
then the question come out, how could i juddge the element type is a String or is an Array?
For me this is actually not a bug, but a useful feature that could be more user friendly or implemented in a better way.
By reading the source code it seems that the eval function has access to the global scope when evaluating expressions in parenthesis.
For instance, if I first define a parsing function, say:
parser = function(param){console.log(param);return '1,2,3,4,5'};
I can do something like that
JSONPath({},data, '$..book[0].category[(parser\(@\))]')
Where data
is the same json as defined in the doc. Would it be possible to implement a regexp parser using this method?
I feel like regular expression is a missing feature in general and I would prefer not to use this hack.
I am experimenting with a fix for smartrics/RestFixture#157 and in the process I am trying jsonpath. Unfortunately, when I try to use it 0.13.0, I get:
ReferenceError: "self" is not defined. (jsonpath-0.13.0.js#449)
I am trying to use JSONPath to parse the JSON returned from GoogleDrive, which looks like: http://pastebin.com/6mTi1wu3
Some of the JSON name / values look like:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "stuff"
I can't get a path with that name to work, and I'm presuming it's because of the "." in the path. I've tried several variants, such as:
$..exportLinks['application/vnd.openxmlformats-officedocument.wordprocessingml.document']
$..exportLinks.application/vnd.openxmlformats-officedocument.wordprocessingml.document
etc.
Nothing has worked. Is there a right way to specify a JSONPath when a name contains "."? Or is this possibly a bug?
Thanks in advance.
Searching for a literal *
with $['*']
yields the following unexpected behavior:
JSONPath({json: {'*': 'star', 'rest': 'rest', 'foo': 'bar'}, path: "$['*']"})
//=> [ 'star' ]
JSONPath({json: {'rest': 'rest', 'foo': 'bar'}, path: "$['*']"})
//=> [ 'rest', 'bar' ]
It's possible to work around this with $[?(@property === '*')]
, but this isn't usable in more complex queries, such as $[?(@property === '*'), rest]
I don't see any tests for using sandbox parameters and they don't seem to work
json = {"books":[{"title":"kids", "author":"adams"},{"title":"action", "author":"johns"}]}
path = '$..books[?(@[email protected])]'
JSONPath({json:json, path : path, sandbox : {param:'kids'}} )
This JSBIN demonstrates this with two test:
http://jsbin.com/cupehuyayo/edit
The first test above is with the param second is with a string
Any help would be appreciated, thank you.
npm does not accept uppercase characters in package names anymore. While it still does work with JSONPath, it leads to a myriad of problems โ especially now with npm 3. Due to the flat dependences this module conflicts with the jsonpath package โ which will not work on case-insensitive file systems.
So the best solution would probably be to republish JSONPath to npm under a new name with lowercase characters, and to make a note in the current version to better use the new package.
npm will likely not fix the issue on their side, they just hope that the problem will go away sometime โ also see this npm issue.
AFAIK, we always use jsonpath for getting value from a json object, is that possible to set a value by jsonpath?
I'm not able to get the following data correctly and it is taking so much time for the task, can you giveme some direction , thanks
(token valid 1 day)
https://api.shodan.io/shodan/host/search?key=MM72AkzHXdHpC8iP65VVEEVrJjp7zkgd&minify=true&query=apache
responsePath key is using JSONPath
"connector": "rest", "debug": "true", "operations": [{ "template": { "method": "GET", "url": "https://api.shodan.io/shodan/host/search?key=MM72AkzHXdHpC8iP65VVEEVrJjp7zkgd&minify=true&query=apache", "headers": { "accepts": "application/json", "content-type": "application/json" }, "query": { "queryw": "{query}" }, "responsePath": "$.." }, }]
As I read on readme about performance issue, I compared it with 3 more implementations of jsonpath. And I found this has the better performance than all.
Check the report here
https://naturalintelligence.github.io/comparejs/?q=jsonpath-comparison
While the sandbox allows for custom filtering (even as custom functions), we might modularize the path parsing so that operator detection and usage can be externally extended.
Due to processing tools like JSLint or the Mozilla add-on validator, etc., throwing flags upon use of "eval", is it possible you could rename this method, or if not, provide an alias so that at least the calling code wouldn't be flagged? Thanks!
I have some JSON which includeds objects-used-as-dictionaries.
Is it possible to get keys from objects using JSONPath, if you do not know the names of the keys in advance of seeing the data?
e.g. I would like to get the results ["France","Spain"] from a JSONPath quesry on:
{
"capitals":{
"France":"Paris",
"Spain":"Madrid"
}
}
It seems like the tag was deleted from npm?
โ npm install [email protected]
npm http GET https://registry.npmjs.org/JSONPath/0.9.3
npm http 304 https://registry.npmjs.org/JSONPath/0.9.3
npm http GET https://registry.npmjs.org/JSONPath/-/JSONPath-0.9.3.tgz
npm http 404 https://registry.npmjs.org/JSONPath/-/JSONPath-0.9.3.tgz
npm ERR! fetch failed https://registry.npmjs.org/JSONPath/-/JSONPath-0.9.3.tgz
npm ERR! Error: 404 Not Found
npm ERR! at WriteStream.<anonymous> (/.nvm/v0.10.22/lib/node_modules/npm/lib/utils/fetch.js:57:12)
npm ERR! at WriteStream.EventEmitter.emit (events.js:117:20)
npm ERR! at fs.js:1596:14
[....]
npm ERR! not ok code 0
The latest version is terribly slow, could you please check it.
Thanks.
A very useful feature (for me anyway) would be a method that lets you get all the paths in an object. Something like:
jsonpath.paths(obj);
.. and have it return an array of unique paths.
The use case for me is to compare two JSON objects and assert that they have the same JSONPaths, ignoring the count and order of how the paths occur in the objects.
I would like to see:
$..*@string()
, we could support something like $..*@type(mySchemaType)
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.