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

Create an array from existing keys #77

Closed
bhardwajdb opened this issue Jun 10, 2018 · 3 comments
Closed

Create an array from existing keys #77

bhardwajdb opened this issue Jun 10, 2018 · 3 comments

Comments

@bhardwajdb
Copy link

bhardwajdb commented Jun 10, 2018

Hi,

I am wondering if there's a way to create an array from existing keys using existing transform functions only ? something like

converting the following JSON

{ 
   "key1": "field1",
   "key2": "field2",
   "key3": "field3"
}

to something like:

{ 
   "arr": ["field1", "field2", "field3"]
}

Another followup would be, Is it possible to create an array at top level ? for example, converting the above JSON to something like:

["field1", "field2", "field3"]

Thanks

@JoshuaC215
Copy link
Collaborator

First case is definitely possible with latest version using shift transform. So for your example the spec to use would be:

{
  "operation": "shift",
  "spec": {
    "arr[+]": "key1",
    "arr[+]": "key2",
    "arr[+]": "key3"
  }
}

Note in this case I used key to append to array, could also specify the indices explicitly.

I believe you can do the same with a top level array by just removing arr in the key strings of the above example, would have to try it to confirm.

@bhardwajdb
Copy link
Author

I am afraid in version 3, i.e. v3 I am getting the following output:

{"arr":["field3"]}

Are you talking about a version later than v3 ?

@JoshuaC215
Copy link
Collaborator

Ah interesting! There's a bug there with the [+] support which is pretty new. I'll open an issue on it.

Meanwhile you can just specify the array index and it will work.

{
  "operation": "shift",
  "spec": {
    "arr[0]": "key1",
    "arr[1]": "key2",
    "arr[2]": "key3"
  }
}

Also it behaves weirdly right now if you try to do a top level array, but there's a workaround with the extract operation. Below spec produces the top level array with your original input.

[{
  "operation": "shift",
  "spec": {
    "arr[0]": "key1",
    "arr[1]": "key2",
    "arr[2]": "key3"
  }
},
{
  "operation": "extract",
  "spec": {
    "path": "arr"
  }
}]

Gonna close this since I just tested it, please do re-open though if it seems to not be working for you. Thanks!

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