From e02ad4d7e8ba96a37bf2448708e7614925939809 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 5 Feb 2021 15:00:46 +1100 Subject: [PATCH] Added space example to docs --- pkg/yqlib/doc/Traverse (Read).md | 16 ++++++++++++++++ pkg/yqlib/operator_traverse_path_test.go | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/pkg/yqlib/doc/Traverse (Read).md b/pkg/yqlib/doc/Traverse (Read).md index f9e43f39..c8b58905 100644 --- a/pkg/yqlib/doc/Traverse (Read).md +++ b/pkg/yqlib/doc/Traverse (Read).md @@ -48,6 +48,22 @@ will output frog ``` +## Keys with spaces +Use quotes with brackets around path elements with special characters + +Given a sample.yml file of: +```yaml +"red rabbit": frog +``` +then +```bash +yq eval '.["red rabbit"]' sample.yml +``` +will output +```yaml +frog +``` + ## Dynamic keys Expressions within [] can be used to dynamically lookup / calculate keys diff --git a/pkg/yqlib/operator_traverse_path_test.go b/pkg/yqlib/operator_traverse_path_test.go index 3171a423..e7d0a73c 100644 --- a/pkg/yqlib/operator_traverse_path_test.go +++ b/pkg/yqlib/operator_traverse_path_test.go @@ -54,6 +54,15 @@ var traversePathOperatorScenarios = []expressionScenario{ "D0, P[{}], (!!str)::frog\n", }, }, + { + description: "Keys with spaces", + subdescription: "Use quotes with brackets around path elements with special characters", + document: `{"red rabbit": frog}`, + expression: `.["red rabbit"]`, + expected: []string{ + "D0, P[red rabbit], (!!str)::frog\n", + }, + }, { skipDoc: true, document: `{"flying fox": frog}`,