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}`,