Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion problems/objects/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Constructing objects
Working with objects
========================================

`jq` can also be used to transform data. For instance, to construct a
Expand All @@ -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

5 changes: 5 additions & 0 deletions problems/objects/problem.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note keys as a built-in alternative!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both methods should return the same result, I think the to_entries is more general.
What do you think I should do with PR?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point (and while we're at it, add to_entries | map(.key) to the set of "valid" solutions—many right answers here!)

What we're showing here is basically the tutorial's "official" solution. We haven't (yet) formed strong opinions around presenting primitive v. higher-level features in this setting, but I think keys will be more practical for learners / new users. Let's show that here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will set it this way. On the other side, my intention was to show the way to get names of keys to work on.

}
]