Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function proposal - entries() #2

Open
glenveegee opened this issue May 16, 2020 · 2 comments
Open

Function proposal - entries() #2

glenveegee opened this issue May 16, 2020 · 2 comments

Comments

@glenveegee
Copy link

glenveegee commented May 16, 2020

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
Copy link
Contributor

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants