mikefarah / yq

yq is a portable command-line YAML, JSON, XML, CSV, TOML and properties processor
https://mikefarah.gitbook.io/yq/
MIT License
12.27k stars 600 forks source link

Only the last element of an array is considered (not working like jq) #1579

Closed bleleve closed 1 year ago

bleleve commented 1 year ago

Version of yq: 4.31.1 Operating system: wsl windows Installed via: binary release

Input Yaml bufbuild_grpc_node.yml:

version: v1
name: buf.build/grpc/node
plugin_version: v1.12.4
source_url: https://github.com/grpc/grpc-node
description: Generates Node client and server stubs for the gRPC framework.
deps:
  - plugin: buf.build/protocolbuffers/js:v3.21.2
output_languages:
  - javascript
registry:
  npm:
    import_style: commonjs
    deps:
    - package: '@grpc/grpc-js'
      version: '^1.8.0'
    - package: 'google-protobuf'
      version: '^3.21.2'
  opts:
  - grpc_js
spdx_license_id: Apache-2.0
license_url: 'https://github.com/grpc/grpc-node/blob/grpc-tools%401.12.4/LICENSE'

Command The command you ran:

yq -r '.registry.npm.deps[]' bufbuild_grpc_node.yml | yq -r '.package + "@" + .version'

Actual behavior

google-protobuf@^3.21.2

Expected behavior

@grpc/grpc-js@^1.8.0
google-protobuf@^3.21.2

Additional context This is working as expected using jq :

yq -r -o json '.registry.npm.deps[]' bufbuild_grpc_node.yml | jq -r '.package + "@" + .version'
mikefarah commented 1 year ago

This is because

yq '.registry.npm.deps[]'

Doesn't produce valid YAML - the output is:

package: '@grpc/grpc-js'
version: '^1.8.0'
package: 'google-protobuf'
version: '^3.21.2'

Where as if you output as json - it produces valid NDJSON - which can be read by both jq and yq (if you tell it to read json via -pj.

To product a valid yaml doc, move the splat to the second command, so that the first still produces a valid doc:

yq '.registry.npm.deps' data1.yaml  | ./yq '.[] | .package + "@" + .version' 
@grpc/grpc-js@^1.8.0
google-protobuf@^3.21.2

Note: -r is default true in yq, so you only need to specify -r=false if you don't want it :)

Edit: fixed command

mikefarah commented 1 year ago

Also, you can of course do this in a single line:

yq '.registry.npm.deps[].package + "@" + .version' data1.yaml

bleleve commented 1 year ago

Thank you Mike ;)

Note: the single line is like this yq '.registry.npm.deps | .[] | .package + "@" + .version' otherwise we have the same problem.

Best regards.