From ca78a746e60ba6360bf915e2fe5884c0e04bb858 Mon Sep 17 00:00:00 2001 From: Kim Toms Date: Tue, 10 Feb 2015 08:33:56 -0500 Subject: [PATCH] Enhance from_entries to better deal with Amazon AWS Tags --- builtin.c | 2 +- docs/content/3.manual/manual.yml | 3 ++- tests/all.test | 6 +++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/builtin.c b/builtin.c index d88c9d7..5ac35cd 100644 --- a/builtin.c +++ b/builtin.c @@ -1036,7 +1036,7 @@ static const char* const jq_builtins[] = { "def recurse_down: recurse;", "def to_entries: [keys_unsorted[] as $k | {key: $k, value: .[$k]}];", - "def from_entries: map({(.key // .Key): (.value // .Value)}) | add | .//={};", + "def from_entries: map({(.key // .Key // .Name): (.value // .Value)}) | add | .//={};", "def with_entries(f): to_entries | map(f) | from_entries;", "def reverse: [.[length - 1 - range(0;length)]];", "def indices($i): if type == \"array\" and ($i|type) == \"array\" then .[$i]" diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index c57c2c4..67dd2c6 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -738,7 +738,8 @@ sections: `from_entries` does the opposite conversion, and `with_entries(foo)` is a shorthand for `to_entries | map(foo) | from_entries`, useful for doing some operation to - all keys and values of an object. + all keys and values of an object. `from_entries` accepts key, Key, + Name, value and Value as keys. examples: - program: 'to_entries' diff --git a/tests/all.test b/tests/all.test index 8961fed..6f2e189 100644 --- a/tests/all.test +++ b/tests/all.test @@ -1035,7 +1035,11 @@ to_entries [{"key":"a", "value":1}, {"key":"b", "value":2}] from_entries -[{"key":"a", "value":1}, {"key":"b", "value":2}] +[{"key":"a", "value":1}, {"Key":"b", "value":2}] +{"a": 1, "b": 2} + +from_entries +[{"key":"a", "Value":1}, {"Name":"b", "value":2}] {"a": 1, "b": 2} with_entries(.key |= "KEY_" + .) -- 2.40.0