- Saved searches
- Use saved searches to filter your results more quickly
- License
- archan937/jsonv.sh
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Convert JSON to CSV with JQ.
- System Requirements
- Installation
- Working with JSON and JQ
- Transform JSON to CSV
- Rich Rose
- Recent Posts
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A Bash command line tool for converting JSON to CSV
License
archan937/jsonv.sh
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
A Bash command line tool for converting JSON to CSV
Run the following command to install jsonv :
$ curl -Ls https://raw.github.com/archan937/jsonv.sh/master/install.sh | bash
To uninstall, run this command:
$ curl -Ls https://raw.github.com/archan937/jsonv.sh/master/uninstall.sh | bash
Note: You also need to install gawk .
Call jsonv and pass the paths of the values used for the CSV columns (comma separated). Optionally, you can pass a prefix for the paths as a second argument.
Parsing examples/simple.json (3 objects) without a path prefix:
$ curl -Ls https://raw.github.com/archan937/jsonv.sh/master/examples/simple.json | jsonv id,name,age 1,"Dagny Taggart",39 8,"Francisco D'Anconia",40 12,"Hank Rearden (a.k.a \"The Tank\")",46
Parsing examples/complex-1.json (1008 objects, file size 764KB) with a path prefix:
$ curl -Ls https://raw.github.com/archan937/jsonv.sh/master/examples/complex-1.json | jsonv person.id,person.name,person.gender,person.balance,person.age,person.company response.data 1,"Anastasia Goodwin","female","$2,623.00",20,"Equitax" 2,"Peters Watson","male","$1,606.00",29,"Blurrybus" 3,"Shirley Gates","female","$3,735.00",25,"Omatom" 4,"Mcgowan Terry","male","$3,121.00",25,"Austech" 5,"Melody Cote","female","$1,519.00",25,"Matrixity" .
Parsing examples/complex-2.json (4734 objects, file size 3.2MB) with a path prefix:
$ curl -Ls https://raw.github.com/archan937/jsonv.sh/master/examples/complex-2.json | jsonv id,name,gender,balance,age,company,address,phone,email,picture,registered response.data 1,"Newton Bush","male","$1,653.00",27,"Xanide","710 Corbin Place, Vivian, Mississippi, 6954","+1 (916) 520-2940","newtonbush@xanide.com","http://placehold.it/32x32","1998-02-17T17:00:12 -01:00" 2,"Ratliff Workman","male","$2,941.00",39,"Bitendrex","680 Menahan Street, Matheny, North Dakota, 3316","+1 (997) 409-2099","ratliffworkman@bitendrex.com","http://placehold.it/32x32","1996-02-02T13:26:48 -01:00" 3,"Hawkins Jacobs","male","$1,584.00",40,"Marqet","349 Joralemon Street, Mansfield, California, 8655","+1 (831) 508-2216","hawkinsjacobs@marqet.com","http://placehold.it/32x32","2012-07-23T15:13:43 -02:00" 4,"Whitfield Maldonado","male","$2,519.00",34,"Tri@Tribalog","966 Moffat Street, Wanamie, Indiana, 3006","+1 (940) 554-2482","whitfieldmaldonado@tri@tribalog.com","http://placehold.it/32x32","2002-11-06T13:40:49 -01:00" 5,"Wise Phelps","male","$1,322.00",23,"Temorak","116 Matthews Court, Orin, Kentucky, 2196","+1 (967) 577-3292","wisephelps@temorak.com","http://placehold.it/32x32","1997-11-20T21:58:48 -01:00" .
Check out this repository first and run the following command:
$ cat examples/complex-3.json | ./jsonv id,name,gender,balance,age,company,address,phone,email,picture,registered response.data 1,"Pearlie Hodge","female","$1,347.00",36,"Zappix","371 Abbey Court, Leola, Iowa, 6919","+1 (966) 515-2796","pearliehodge@zappix.com","http://placehold.it/32x32","2004-05-18T01:48:01 -02:00" 2,"Tessa Saunders","female","$1,398.00",24,"Zounds","399 Provost Street, Ironton, Alaska, 4909","+1 (807) 501-3914","tessasaunders@zounds.com","http://placehold.it/32x32","2001-11-27T14:20:43 -01:00" 3,"Clarissa Hawkins","female","$2,974.00",33,"Recrisys","566 Drew Street, Elliston, Maine, 9520","+1 (867) 510-2236","clarissahawkins@recrisys.com","http://placehold.it/32x32","2002-09-12T12:01:35 -02:00" 4,"Lauren Sexton","female","$2,282.00",26,"Lunchpad","461 Lloyd Street, Deercroft, Arkansas, 9022","+1 (937) 509-3538","laurensexton@lunchpad.com","http://placehold.it/32x32","1998-07-01T19:13:55 -02:00" 5,"Hutchinson Ayers","male","$1,697.00",20,"Mangelica","129 Tabor Court, Freeburn, North Dakota, 2921","+1 (823) 542-2366","hutchinsonayers@mangelica.com","http://placehold.it/32x32","1997-11-20T04:39:49 -01:00" .
In this example, examples/complex-3.json contains 19828 objects (file size 13.6MB) and is parsed in about 20 seconds.
When using jsonv , it writes log messages to log file jsonv.log located within the same directory as jsonv itself.
===================== [2013-12-07 04:10:29] Writing JSON file [2013-12-07 04:10:31] Writing tokens file [2013-12-07 04:10:32] Deriving keys [2013-12-07 04:10:32] Deriving prefix [2013-12-07 04:10:32] Counting entries [2013-12-07 04:10:32] Writing map file [2013-12-07 04:10:32] Deriving line format [2013-12-07 04:10:32] Compiling CSV output [2013-12-07 04:10:32] Done. =====================
jsonv heavily makes use of JSON.awk created by Stepk (step-):
Thanks Dominic Tarr (@dominictarr) for creating JSON.sh on which JSON.awk is based on:
Thanks GNU Project (http://www.gnu.org) for creating gawk :
Copyright (c) 2013 Paul Engel, released under the MIT license
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the «Software»), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED «AS IS», WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
About
A Bash command line tool for converting JSON to CSV
Convert JSON to CSV with JQ.
jq is like sed for JSON data — you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.
Being able to move between different file format is quite a common task, so I was delighted to find a quick and easy method for JSON/CSV. In the following blog post learn how to use jq to transform JSON data into the CSV format. Once the data is in CSV format, it should be a simple matter to import into other applications (e.g. databases, spreadsheets, etc).
- The example assumes you have a linux environment.
- An example JSON file is provided to illustrate the commands required.
System Requirements
Installation
Working with JSON and JQ
Working with jq takes all the hardwork out of handling json. Lets look at an example
1 2 "stock": [ 3 4 "id": "ABC0001", 5 "item": "Cable", 6 "description": "USB-A Cable" 7 >, 8 9 "id": "ABC0002", 10 "item": "Cable", 11 "description": "USB-B Cable" 12 >, 13 14 "id": "ABC0003", 15 "item": "Cable", 16 "description": "USB-C Cable" 17 > 18 ], 19 "timestamp": "2021-03-06T14:55:45.680Z", 20 "author": "Rich Rose" 21>
NOTE: Here jq not only parses the file, it also validates it.
NOTE: Here jq is being asked to read the top level element timestamp
NOTE: Here jq is being asked to read the top level element stock . We note that the stock item represents an array. Each array item contains an element (e.g. id , item and description )
Transform JSON to CSV
Sometimes it necessary to transform a JSON file into another format. One common format is comma separated values (CSV). Fortunately jq is able to help with this task and make data transformation very simple. Lets use the same example json file to transform the stock items into a CSV file.
Which will provide an output similar to that below:
1 2 "id": "ABC0001", 3 "item": "Cable", 4 "description": "USB-A Cable" 5> 6 7 "id": "ABC0002", 8 "item": "Cable", 9 "description": "USB-B Cable" 10> 11 12 "id": "ABC0003", 13 "item": "Cable", 14 "description": "USB-C Cable" 15>
- Select the information that is to be displayed in the CSV. In our example, the fields id , item and description are used.
1jq '.stock[] | [.id, .item, .description]' test.json
NOTE: Here jq is being asked to read the top level element stock . We then use the pipe command (i.e. | ) to pass the result as a parameter to the next command and perform another parse. In the second parse, we indicate we want specific information (i.e. .id , .item and .description ).
The above command should provide an output similar to that below:
1[ 2 "ABC0001", 3 "Cable", 4 "USB-A Cable" 5] 6[ 7 "ABC0002", 8 "Cable", 9 "USB-B Cable" 10] 11[ 12 "ABC0003", 13 "Cable", 14 "USB-C Cable" 15]
- jq now can be told that we would like the output shown as a CSV. To achieve that we introduce the keyword @csv .
1jq '.stock[] | [.id, .item, .description] | @csv' test.json
NOTE: We are again using the | to chain the previous command results, this time to @csv .
The above command should provide an output similar to that below:
1"\"ABC0001\",\"Cable\",\"USB-A Cable\"" 2"\"ABC0002\",\"Cable\",\"USB-B Cable\"" 3"\"ABC0003\",\"Cable\",\"USB-C Cable\""
- The final step is to tell jq that we would like to view the raw output. Add the -r parameter and we have the desired formatting.
1jq -r '.stock[] | [.id, .item, .description] | @csv' test.json
The above command should provide an output similar to that below:
1"ABC0001","Cable","USB-A Cable" 2"ABC0002","Cable","USB-B Cable" 3"ABC0003","Cable","USB-C Cable"