@daz.is/jmespath

中文文档
1.3.1发布于 2020/07/01Apache-2.0CommonJS零依赖近 30 天下载 2429

JMESPath implementation in javascript

原始使用文档
文档目录8 节
  1. Install
  2. FORK
  3. About
  4. Adding custom functions
  5. Key-only MultiSelect Hashes
  6. Optional arguments to functions
  7. JMESPath+
  8. More Resources

jmespath.js

Build Status

Install

npm install --save @daz.is/jmespath

FORK

NB: This is a fork of the original JavaScript implementation of JMES Path. The original is now believed to be unmaintained, so this fork adds several useful features:

  • define custom functions
  • cache parsed expressions
  • access the root element using $

About

jmespath.js is a javascript implementation of JMESPath, which is a query language for JSON. It will take a JSON document and transform it into another JSON document through a JMESPath expression.

Using jmespath.js is really easy. There's a single function you use, jmespath.search:

var jmespath = require('jmespath');
jmespath.search({foo: {bar: {baz: [0, 1, 2, 3, 4]}}}, "foo.bar.baz[2]")

// output = 2

In the example we gave the search function input data of {foo: {bar: {baz: [0, 1, 2, 3, 4]}}} as well as the JMESPath expression foo.bar.baz[2], and the search function evaluated the expression against the input data to produce the result 2.

The JMESPath language can do a lot more than select an element from a list. Here are a few more examples:

jmespath.search({foo: {bar: {baz: [0, 1, 2, 3, 4]}}}, "foo.bar")

// { baz: [ 0, 1, 2, 3, 4 ] }

jmespath.search({"foo": [{"first": "a", "last": "b"},
                           {"first": "c", "last": "d"}]},
                  "foo[*].first")

// [ 'a', 'c' ]

jmespath.search({"foo": [{"age": 20}, {"age": 25},
                           {"age": 30}, {"age": 35},
                           {"age": 40}]},
                  "foo[?age > `30`]")

// [ { age: 35 }, { age: 40 } ]

Adding custom functions

Custom functions can be added to the JMESPath runtime by using the decorate() function:

function customFunc(resolvedArgs) {
  return resolvedArgs[0] + 99;
}
var extraFunctions = {
  custom: {_func: customFunc, _signature: [{types: [jmespath.types.TYPE_NUMBER]}]},
};
jmespath.decorate(extraFunctions);

The value returned by the decorate function is a curried function (takes arguments one at a time) that takes the search expression first and then the data to search against as the second parameter:

var value = jmespath.decorate(extraFunctions)('custom(`1`)')({})
// value = 100

Because the return value from decorate() is a curried function the result of compiling the expression can be cached and run multiple times against different data:

var expr = jmespath.decorate({})('a');
// expr is now a cached compiled version of the search expression
var value = expr({ a: 1 });
assert.strictEqual(value, 1);
value = expr({ a: 2 });
assert.strictEqual(value, 2);

Key-only MultiSelect Hashes

I have changed the grammar slightly to allow multi-select hashes to be specified using only the keys.

var data = { one: 'first',  two: 'second' };
var result = jmespath.search(data, '{ one }');
strictDeepEqual(result, { one: 'first' });

Optional arguments to functions

The latest version (v1.3) adds the ability for functions (including custom functions added via decorate) to have optional arguments. To activate this, set optional: true in the function signature.

JMESPath+

For more examples of custom functions, and intregrating Lodash, see JMESPath+.

More Resources

The example above only show a small amount of what a JMESPath expression can do. If you want to take a tour of the language, the best place to go is the JMESPath Tutorial.

One of the best things about JMESPath is that it is implemented in many different programming languages including python, ruby, php, lua, etc. To see a complete list of libraries, check out the JMESPath libraries page.

And finally, the full JMESPath specification can be found on the JMESPath site.

版本与发布记录

@daz.is/jmespath 共发布 9 个版本,首次发布于 2020/02/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.1latest2020/07/016 年前
1.3.02020/07/016 年前
1.2.12020/06/306 年前
1.2.02020/06/306 年前
1.1.22020/02/286 年前
1.1.12020/02/286 年前

关于 @daz.is/jmespath 的常见问题

@daz.is/jmespath 是什么?

JMESPath implementation in javascript

如何安装 @daz.is/jmespath?

在项目目录运行 npm install @daz.is/jmespath,或使用 pnpm add / yarn add / bun add。

@daz.is/jmespath 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/daz.is__jmespath 是否存在。

@daz.is/jmespath 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@daz.is/jmespath 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 0.6.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@daz.is/jmespath 可以商用吗?

@daz.is/jmespath 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@daz.is/jmespath 的下载量有多少?

根据 npm 官方下载统计,@daz.is/jmespath 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,429 次,日均约 81 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@daz.is/jmespath 还在维护吗?

最新版本 1.3.1 发布于 2020/07/01(6 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/02/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。