jmespath / jmespath.jep

Proposals for extending the JMESPath Language.
8 stars 5 forks source link

Function proposal - entries() #2

Open glenveegee opened 4 years ago

glenveegee commented 4 years ago

Given a array or object output an array with values indexed on keys:

For example in Javascript one might implement it as follows:

registerFunction(
  'entries',
  ([resolvedArgs]) => {
    return Object.entries(resolvedArgs);
  },
  [{ types: [TYPE_OBJECT, TYPE_ARRAY] }],
);

Examples:

// With ARRAY input:

jmespath.search([1,"2", null, ["a", 2, "foo"], {b: "z"}, true, false], "entries(@)")
// OUTPUTS:   [
//     [ '0', 1 ],
//     [ '1', '2' ],
//     [ '2', null ],
//     [ '3', [ 'a', 2, 'foo' ] ],
//     [ '4', { b: 'z' } ],
//     [ '5', true ],
//     [ '6', false ]
//   ]
// With **OBJECT** input:

jmespath.search({a: 1, b: [2, 3], c: null, 4: {x: 0}}, 'entries(@)')
// OUTPUTS: [
//     [ '4', { x: 0 } ],
//     [ 'a', 1 ],
//     [ 'b', [ 2, 3 ] ],
//     [ 'c', null ]
//   ]
springcomp commented 2 years ago

This looks very similar to the items function that was proposed a while back. We plan to include this in the next version of our specification.

glenveegee commented 2 years ago

It's slightly different from the items proposal in that it would be polymorphic by working on both JSON objects and JSON arrays.