Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yq does not parse property path correctly ... ? #2266

Closed
JoshMcCullough opened this issue Jan 23, 2025 · 5 comments
Closed

yq does not parse property path correctly ... ? #2266

JoshMcCullough opened this issue Jan 23, 2025 · 5 comments
Labels

Comments

@JoshMcCullough
Copy link

Describe the bug
I must be doing something wrong here as this seems very basic. It appears that nested properties are not parsed correctly? See below.

Version of yq: 4.45.1
Operating system: RHEL8
Installed via: binary

Input Yaml
test.yml:

test:
  blah: x
  some.data: z

Command

# This works correctly:
> yq -r '.test.blah' test.yml
x

# This does not (expected "z"):
> yq -r '.test.some.data' test.yml
null

# Quoting the compound path part works:
> yq -r '.test."some.data"' test.yml
z

Actual behavior
Querying a compound path part (test.some.data) does not traverse the path.

Expected behavior
Querying a compound path part (test.some.data) should traverse the path without the need for quoting.

Additional context
The test.yml file above includes two ways to write nested paths. A given YAML file could look either way:

test:
  some.data: x

-- or --

test:
  some:
    data: x

Both are valid YAML, the end user wouldn't know which format is in place within a given YAML file (without looking), so querying test.some.data should work for both cases.

@jandubois
Copy link
Contributor

jandubois commented Jan 25, 2025

The test.yml file above includes two ways to write nested paths. A given YAML file could look either way:

test:
  some.data: x

-- or --

test:
  some:
    data: x

This is not correct; the 2 YAML files are not equivalent. Rewriting them in JSON makes it obvious:

{ "test": { "some.data": "x" } }

-- vs --

{ "test": { "some": { "data": "x" } } }

The dot is simply part of the key name; it does not split into a sub-object at the dot. I don't see any incorrect behaviour in this issue.

@JoshMcCullough
Copy link
Author

I don't think rewriting in JSON is a valid way to prove that this is correct behavior. YML is not JSON.

In YML, my examples are equivalent. . is a path separator in YML. Or perhaps I've been spoiled by Spring Boot which correctly traverses the path in either case.

@jandubois
Copy link
Contributor

In YML, my examples are equivalent. . is a path separator in YML.

The . has not special meaning in key names in YAML. Please check the YAML spec!

Or perhaps I've been spoiled by Spring Boot which correctly traverses the path in either case.

Yes, this happens after YAML processing. Spring converts the YAML config into properties format before parsing it again. So both files end up as:

test.some.data=x

But that is unrelated to YAML parsing.

@jandubois
Copy link
Contributor

Spring converts the YAML config into properties format before parsing it again.

Since yq has support for properties as well, you could do the same thing like this:

cat test.yaml
test:
  some.data: xyq -o=props test.yaml
test.some.data = xyq -o=props test.yaml | yq -p=props .test.some.data
x

It turns test.yaml into properties format, then reads the properties format and queries it.

@JoshMcCullough
Copy link
Author

Okay, I'll close this, then. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants