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

jsonp: add jsonp command allowing non-nested JSON to CSV conversion with Polars #1880

Merged
merged 2 commits into from
Jun 15, 2024

Conversation

rzmk
Copy link
Collaborator

@rzmk rzmk commented Jun 14, 2024

#1878 covers the example and use case for this new command qsv jsonp.

qsv-jsonp-demo

Excerpt from qsv jsonp --help

Convert non-nested JSON to CSV (polars feature only). You may provide JSON data either from stdin or a file path. This command may not work with nested JSON data.

As a basic example, say we have a file fruits.json with contents:

[
    {
        "fruit": "apple",
        "price": 2.5
    },
    {
        "fruit": "banana",
        "price": 3.0
    }
]

To convert it to CSV format, run:

qsv jsonp fruits.json

And the following is printed to the terminal:

fruit,price
apple,2.5
banana,3.0

If fruits.json was provided using stdin then either use - or do not provide a file path. For example:

cat fruits.json | qsv jsonp -

Sample usage

Create a CSV file fruits.csv based on fruits.json:

cat fruits.json | qsv jsonp -o fruits.csv

Print to terminal the converted CSV data from a JSON file you may pick from a file dialog which is filtered to show .json files only:

qsv prompt -F json | qsv jsonp

Potential improvements

  • This command relies on the polars feature being enabled, so an implementation without the feature being enabled and converting this command from jsonp to json is a potential improvement.
  • Allowing nested JSON file conversion (may be customizable with options).

src/cmd/jsonp.rs Fixed Show fixed Hide fixed
src/cmd/jsonp.rs Fixed Show fixed Hide fixed
src/cmd/jsonp.rs Fixed Show fixed Hide fixed
@jqnatividad jqnatividad merged commit 7cd8fcf into master Jun 15, 2024
15 checks passed
@jqnatividad
Copy link
Owner

Thanks for this new command @rzmk!
With this, we now have increased JSON compatibility beyond the less common JSONL format.

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

Successfully merging this pull request may close these issues.

None yet

2 participants