在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:dchester/jsonpath开源软件地址:https://github.com/dchester/jsonpath开源编程语言:JavaScript 100.0%开源软件介绍:jsonpathQuery JavaScript objects with JSONPath expressions. Robust / safe JSONPath engine for Node.js. Query Examplevar cities = [
{ name: "London", "population": 8615246 },
{ name: "Berlin", "population": 3517424 },
{ name: "Madrid", "population": 3165235 },
{ name: "Rome", "population": 2870528 }
];
var jp = require('jsonpath');
var names = jp.query(cities, '$..name');
// [ "London", "Berlin", "Madrid", "Rome" ] InstallInstall from npm: $ npm install jsonpath JSONPath SyntaxHere are syntax and examples adapted from Stefan Goessner's original post introducing JSONPath in 2007.
Given this sample data set, see example expressions below: {
"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
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}, {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
} Example JSONPath expressions:
Methodsjp.query(obj, pathExpression[, count])Find elements in var authors = jp.query(data, '$..author');
// [ 'Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien' ] jp.paths(obj, pathExpression[, count])Find paths to elements in var paths = jp.paths(data, '$..author');
// [
// ['$', 'store', 'book', 0, 'author'] },
// ['$', 'store', 'book', 1, 'author'] },
// ['$', 'store', 'book', 2, 'author'] },
// ['$', 'store', 'book', 3, 'author'] }
// ] jp.nodes(obj, pathExpression[, count])Find elements and their corresponding paths in var nodes = jp.nodes(data, '$..author');
// [
// { path: ['$', 'store', 'book', 0, 'author'], value: 'Nigel Rees' },
// { path: ['$', 'store', 'book', 1, 'author'], value: 'Evelyn Waugh' },
// { path: ['$', 'store', 'book', 2, 'author'], value: 'Herman Melville' },
// { path: ['$', 'store', 'book', 3, 'author'], value: 'J. R. R. Tolkien' }
// ] jp.value(obj, pathExpression[, newValue])Returns the value of the first element matching jp.parent(obj, pathExpression)Returns the parent of the first matching element. jp.apply(obj, pathExpression, fn)Runs the supplied function var nodes = jp.apply(data, '$..author', function(value) { return value.toUpperCase() });
// [
// { path: ['$', 'store', 'book', 0, 'author'], value: 'NIGEL REES' },
// { path: ['$', 'store', 'book', 1, 'author'], value: 'EVELYN WAUGH' },
// { path: ['$', 'store', 'book', 2, 'author'], value: 'HERMAN MELVILLE' },
// { path: ['$', 'store', 'book', 3, 'author'], value: 'J. R. R. TOLKIEN' }
// ] jp.parse(pathExpression)Parse the provided JSONPath expression into path components and their associated operations. var path = jp.parse('$..author');
// [
// { expression: { type: 'root', value: '$' } },
// { expression: { type: 'identifier', value: 'author' }, operation: 'member', scope: 'descendant' }
// ] jp.stringify(path)Returns a path expression in string form, given a path. The supplied path may either be a flat array of keys, as returned by var pathExpression = jp.stringify(['$', 'store', 'book', 0, 'author']);
// "$.store.book[0].author" Differences from Original ImplementationThis implementation aims to be compatible with Stefan Goessner's original implementation with a few notable exceptions described below. Evaluating Script ExpressionsScript expressions (i.e, GrammarThis project uses a formal BNF grammar to parse JSONPath expressions, an attempt at reverse-engineering the intent of the original implementation, which parses via a series of creative regular expressions. The original regex approach can sometimes be forgiving for better or for worse (e.g., Other Minor DifferencesAs a result of using a real parser and static evaluation, there are some arguable bugs in the original library that have not been carried through here:
License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论