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

flatten array of maps to create tsv format #2271

Open
ckuenne opened this issue Jan 29, 2025 · 3 comments
Open

flatten array of maps to create tsv format #2271

ckuenne opened this issue Jan 29, 2025 · 3 comments

Comments

@ckuenne
Copy link

ckuenne commented Jan 29, 2025

i'm trying to collapse/flatten a structure so i can convert to tsv format.

Version of yq: 4.45.1
Operating system: Debian 11
Installed via: wget binary release

test.yml:

- name: Miller, Tim
  email: Tim.Miller@xy.com
  user_metadata:
    username: tmiller
    x: 1
- name: Jones, Karen
  email: Karen.Jones@z.com
  user_metadata:
    username: kjones
    y: 2

this works as long as i ignore the map "user_metadata":

yq -o tsv '.[] |= pick(["name","email"])' test.yml
->
name	email
Miller, Tim	Tim.Miller@xy.com
Jones, Karen	Karen.Jones@z.com

but i also need to flatten the map inside the array (basically remove the first level) and then select from the remaining keys. this is my desired output:

name	email	username
Miller, Tim	Tim.Miller@xy.com	tmiller
Jones, Karen	Karen.Jones@z.com	kjones

it's probably not difficult but i'm out of ideas...

@jandubois
Copy link
Contributor

If you want just this specific operation and not a generic flattening, you can just pick the fields yourself instead of using pick:

yq -o tsv '.[] |= {"name":.name, "email":.email, "username":.user_metadata.username}' test.yml
name	email	username
Miller, Tim	Tim.Miller@xy.com	tmiller
Jones, Karen	Karen.Jones@z.com	kjones

@jandubois
Copy link
Contributor

This should be moved to "Discussions" as it is not a bug / feature request...

@ckuenne
Copy link
Author

ckuenne commented Feb 7, 2025

that solves my current use case already, thx a lot! new i just missed something basic.

but it would indeed be cool to have a more generic version that works with less hardcoding.

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

No branches or pull requests

2 participants