Package: jac-format
JAC (JSON as CSV) FormatThe JAC format makes it easy to convert to and from JSON and CSV file formats, while giving application developers a lot of flexibility to customize how easy it is to modify the CSV file for end users. Any JSON object can be represented as a JAC CSV..jac.csvfiles are always valid CSVs.An example JAC file is shown below:path.namedogs.0dogs.1myNameJohnfriends.0StacyRufusfriends.1PaulMr. FluffsWhimpersWhen this file is converted into JSON, it becomes:{"myName":"John","friends":[{"name":"Stacy","dogs":["Rufus"]},{"name":"Paul","dogs":["Mr. Fluffs","Whimpers"]}]}Usage with Javascriptnpm install jac-formatconstJAC=require("jac-format")letcsvString=JAC.toCSV({fruit:[{name:"apple"},{name:"lemon"}],},{rows:["fruit.0","fruit.1"],// optionalcolumns:["name"],// optional})// "path,name\r\nfruit.0,apple\r\nfruit.1,lemon"// ┌───┬───────────┬─────────┐// │   │     A     │    B    │// ├───┼───────────┼─────────┤// │ 1 │  'path'   │ 'name'  │// │ 2 │ 'fruit.0' │ 'apple' │// │ 3 │ 'fruit.1' │ 'lemon' │// └───┴───────────┴─────────┘// You can also use thisJAC.toJSON(csvString)// > { "fruit": [{ "name": "apple" }, { "name": "lemon" }] }// JAC.fromCSV === JAC.toJSON// JAC.fromJSON === JAC.toCSVUsage with Pythonpip install jac_formatimportjac_formatasjaccsv_string=jac.to_csv({"fruit":[{"name":"apple"},{"name":"lemon"}],},rows=["fruit.0","fruit.1"],columns=["name"])# > csv_string# "path,name\r\nfruit.0,apple\r\nfruit.1,lemon"# ┌───┬───────────┬─────────┐# │   │     A     │    B    │# ├───┼───────────┼─────────┤# │ 1 │  'path'   │ 'name'  │# │ 2 │ 'fruit.0' │ 'apple' │# │ 3 │ 'fruit.1' │ 'lemon' │# └───┴───────────┴─────────┘jac.to_json(csv_string)# { "fruit": [{ "name": "apple" }, { "name": "lemon" }] }RulesJAC CSV files are validRFC4180 CSVsjac_csv_pathorpathis the first column, first cellThe first column contains the first path segment (except for thejac_csv_pathcell)The first row (header) contains the second path segment (except for thejac_csv_pathcell)Each value cell of a JAC CSV can bean empty cella stringa JSON objectnulla numbera JSON arrayColumns right of the "path" column are applied in order from left to right. Each row creates an object. This object is then set at the path of the first column.A path can be traversed with either square bracket notation or dot notationIn dot notation, the usage of a number indicates the index of an array (a["1"].0is equivalent toa["1"][0])If an array has undefined values, those values are set tonullA value cell's path is constructed by taking the leftmost cell of of a row (in the path column) and appending the topmost header to itAutomatic Indexing with "*"Automatic indexing makes it easier to add and delete rows because index numbers don't need to be adjusted.These tables are equivalent when converted to JSON:path.namedogs.*dogs.*myNameJohnfriends.*StacyRufusfriends.*PaulMr. FluffsWhimperspath.namedogs.0dogs.1myNameJohnfriends.0StacyRufusfriends.1PaulMr. FluffsWhimpersIf "*" are replaced by the smallest index in the path segment that's not already taken. There are two appropriate syntaxes, "[*]" or ".*". For a row, only the path segments in the row are considered (i.e. the header is converted into indicies without any information from thepathcolumn).You can also use the*to refer to the last object created matching the prefix preceding the star. The example below is equivalent to the two tables above.path.namemyNameJohnfriends.*Stacyfriends.*.dogs.0Rufusfriends.*Paulfriends.*.dogs.0Mr. Fluffsfriends.*.dogs.1WhimpersPros & ConsThe flexibility of the JAC CSV format allows applications that output JAC CSV to give the user CSV data in a "flattening" that is most convenient for the application i.e. Columns can be created to make it easy for the user to modify the data.As a result of the flexibility in the JAC CSV format, one JSON file can have almost an infinite amount of CSV variations.Column order matters because it determines how the CSV is merged back into JSON