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

csv2json --select "path.to.object" to export only object #261

Open
porg opened this issue Aug 28, 2024 · 2 comments
Open

csv2json --select "path.to.object" to export only object #261

porg opened this issue Aug 28, 2024 · 2 comments

Comments

@porg
Copy link

porg commented Aug 28, 2024

Foreword

  • Thanks for providing the app! I use your app via CLI.
  • I looked into the manpage but found no options to achieve my goal.
  • So I think this is de facto a feature request

User Story

From a large JSON file containing a lot of stuff I want the option to select only a particular object for export to CSV.

With a syntax like --select "path.to.object"

Or another argument name you deem appropriate as the mnemonics -s and -S are both already taken.

Given example.json

{
  "version": "1.5",
  "global_option_a": "something",
  "global_option_b": "else",
  "global_option_c": 123,
  "people": [
    {
      "firstname": "Jon",
      "lastname": "Doe",
      "birthday": "1978-02-19",
      "gender": "m"
    },
    {
      "firstname": "Jane",
      "lastname": "Doe",
      "birthday": "1982-04-21",
      "gender": "f"
    },
    {
      "firstname": "Dave",
      "lastname": "Meyer",
      "birthday": "1964-12-03",
      "gender": "m"
    }
  ],
  "locations": {
    "Europe": [
      {
        "ID": "1",
        "Name": "London"
      },
      {
        "ID": "2",
        "Name": "Paris"
      },
      {
        "ID": "3",
        "Name": "Madrid"
      }
    ],
    "Asia": [
      {
        "ID": "4",
        "Name": "Tokio"
      },
      {
        "ID": "5",
        "Name": "Seoul"
      }
    ]
  }
}

To get people.csv I'd run…

$ json2csv example.json --select "people" > people.csv

which results in:

firstname,lastname,birthday,gender
Jon,Doe,1978-02-19,m
Jane,Doe,1982-04-21,f
Dave,Mayer,1964-12-03,m

To get locations-asia.csv I'd run

$ json2csv example.json --select "locations.Asia" > locations-asia.csv

which results in:

ID,Name
4,Tokio
5,Seoul
@porg
Copy link
Author

porg commented Aug 28, 2024

On my second read of the manpage I realized that --keys is probably getting close to what I need. But still I cannot achieve my desired goal. I'm doing something wrong it seems:


$ json2csv example.json --keys people   
people
"[{""firstname"":""Jon"",""lastname"":""Doe"",""birthday"":""1978-02-19"",""gender"":""m""},{""firstname"":""Jane"",""lastname"":""Doe"",""birthday"":""1982-04-21"",""gender"":""f""},{""firstname"":""Dave"",""lastname"":""Meyer"",""birthday"":""1964-12-03"",""gender"":""m""}]"
  • All in one line
  • On first glance seems not to be CSV but somehow "wrongly/strangely escaped JSON"

$ json2csv example.json --keys people --unwind-arrays
people
"{""firstname"":""Jon"",""lastname"":""Doe"",""birthday"":""1978-02-19"",""gender"":""m""}"
"{""firstname"":""Jane"",""lastname"":""Doe"",""birthday"":""1982-04-21"",""gender"":""f""}"
"{""firstname"":""Dave"",""lastname"":""Meyer"",""birthday"":""1964-12-03"",""gender"":""m""}"
  • Managed to get 1 record per row
  • But still this ain't CSV and there ain't the intended headers.
  • Possibly that flag is not intended for my desired goal, as the "people" on top indicates this indeed is a CSV but with only a single column, and per each column it has a JSON with all it's attributes escaped.

So yes, I'm out of wits. Looking forward to your reaction!

@porg
Copy link
Author

porg commented Sep 22, 2024

@mrodrig any input? Would be appreciated! Thanks!

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

1 participant