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

Parallel Arrays to Objects #609

Closed
vito-c opened this issue Oct 30, 2014 · 11 comments
Closed

Parallel Arrays to Objects #609

vito-c opened this issue Oct 30, 2014 · 11 comments
Labels

Comments

@vito-c
Copy link

vito-c commented Oct 30, 2014

input: echo '{"vals":["20","10"],"names":["thing","zip"]}' 
desired output: [ {"val":20,"name":"thing"},{"val":10,"name":"zip"} ]

I have tried a number of things to convert these two arrays into an object but haven't had much luck. I was thinking maybe I could add an index counter and then map off that but I don't even know if there is a way to do that. Thanks :)

@pkoppstein
Copy link
Contributor

def input:{"vals":["20","10"],"names":["thing","zip"]};
def output:
  .vals as $vals | .names as $names
  | reduce range(0; $names|length) as $i
      ([]; . + [{ "val": $vals[$i], "name": $names[$i] }]);

input| output
$ jq -n -c -f unzip.jq
[{"val":"20","name":"thing"},{"val":"10","name":"zip"}]

@ghost
Copy link

ghost commented Oct 30, 2014

It seems to me that you're looking for a version of the typical zip function that acts over objects. I once had to do this in JavaScript; maybe it's a good time to release it as a separate library and/or port it to jq!

For obtaining the inputs and outputs you specified, here's a simple jq snippet, using transpose:

jq '[.vals, .names] | transpose | map({"val": .[0], "name": .[1]})'

@vito-c
Copy link
Author

vito-c commented Oct 30, 2014

@pkoppstein the reduce worked! and it's a great example of how to use the reduce and do some tricky stuff. Thanks!
@slapresta I don't seem to have the transpose function what version of jq is that in? :)

@vito-c vito-c closed this as completed Oct 30, 2014
@ghost
Copy link

ghost commented Oct 30, 2014

@vito-c No idea when it was introduced, but it's been on master for a while. I seem to recall it being on 1.4, but I'm probably wrong about this.

@wtlangford
Copy link
Contributor

Transpose is new in 1.5, which is currently just master.

On Thu, Oct 30, 2014, 15:51 vito-c notifications@github.com wrote:

@pkoppstein https://github.com/pkoppstein the reduce worked! and it's a
great example of how to use the reduce and do some tricky stuff. Thanks!
@slapresta https://github.com/slapresta I don't seem to have the
transpose function what version of jq is that in? :)


Reply to this email directly or view it on GitHub
#609 (comment).

@vito-c
Copy link
Author

vito-c commented Oct 30, 2014

Ah ic time to: brew update; brew uninstall jq; brew install jq --HEAD

@ghost
Copy link

ghost commented Oct 30, 2014

For the record, this one solves the general case:

def zip_object:
    to_entries | sort_by(.key) |
    map(.key) as $keys |
    map(.value) | transpose |
    map(
        [$keys, .] | transpose |
        map({key: .[0], value: .[1]}) | from_entries
    )
;

And you would use it as follows:

jq -n '{"number": ["20", "10", "30"], "stuff": ["thing", "zip", "bacon"], "foo": ["bar", "baz", "quux"]} | zip_object'

@vito-c
Copy link
Author

vito-c commented Oct 30, 2014

@slapresta where do I need to put these defines so that they are picked up by my commandline jq?

@ghost
Copy link

ghost commented Oct 30, 2014

IIRC you could put them at ~/.jq or something like that, although you can just put the definition of the one you need at the beginning of the corresponding command.

@vito-c
Copy link
Author

vito-c commented Oct 31, 2014

@slapresta is it possible for me add tags to issues? I'd like to look through some of the issues and tag them as examples or use cases

@nicowilliams
Copy link
Contributor

If not let me know what tags for what issues (open an issue for it, even)
and I'll tag them.

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

No branches or pull requests

5 participants