From c295de09ea738d71b16bd18b4ff169828297d584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20C=CC=8Cech?= Date: Tue, 19 Oct 2021 16:24:53 +0200 Subject: [PATCH] Introduce obtaining list of object's keys. --- problems/objects/README.md | 8 +++++++- problems/objects/problem.json | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/problems/objects/README.md b/problems/objects/README.md index 9d8cab0..3655010 100644 --- a/problems/objects/README.md +++ b/problems/objects/README.md @@ -1,4 +1,4 @@ -Constructing objects +Working with objects ======================================== `jq` can also be used to transform data. For instance, to construct a @@ -19,3 +19,9 @@ it will be copied into the output object: $ echo '{"foo": { "bar": "a value" }}' \ | jq '{ foo, why: "demonstration copy" }' +`jq` can also transform object into a list of `key` and `value` pairs +with `to_entries` function: + + $ echo '{"banana": "yellow", "blueberry": "blue"}' \ + | jq to_entries + diff --git a/problems/objects/problem.json b/problems/objects/problem.json index 075bbe0..09b1923 100644 --- a/problems/objects/problem.json +++ b/problems/objects/problem.json @@ -18,6 +18,11 @@ "prompt": "Construct a new object with the 'name' field renamed to 'title'", "dataset": "product", "solution": "{ price, title: .name}" + }, + { + "prompt": "Construct a list of all properties (keys) of a product", + "dataset": "product", + "solution": "to_entries | [.[].key]" } ]