To not have "data" as root level but just pure array of data.
Created
August 18, 2023 03:18
-
-
Save vuon9/345592c18f6dfd29b364cf268ccfca8d to your computer and use it in GitHub Desktop.
Sequel Ace bundle - Copy as JSON (modified)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cat | /usr/bin/perl -e ' | |
# read first line to get the column names (header) | |
$firstLine = <>; | |
# bail if nothing could read | |
if(!defined($firstLine)) { | |
exit 0; | |
} | |
# store the column names | |
chomp($firstLine); | |
$firstLine =~ s/\"/\\\"/g; # escape " | |
@header = split(/\t/, $firstLine); | |
$h_cnt = $#header; # number of columns | |
# get the column definitions | |
open(META, $ENV{"SP_BUNDLE_INPUT_TABLE_METADATA"}) or die $!; | |
@meta = (); | |
while(<META>) { | |
chomp(); | |
my @arr = split(/\t/); | |
push @meta, \@arr; | |
} | |
close(META); | |
# read row data of each selected row | |
$rowData=<>; | |
while($rowData) { | |
print "{\n"; | |
# remove line ending | |
chomp($rowData); | |
# escape " | |
$rowData=~s/\"/\\\"/g; | |
# split column data which are tab-delimited | |
@data = split(/\t/, $rowData); | |
for($i=0; $i<=$h_cnt; $i++) { | |
# re-escape \t and \n | |
$cellData = $data[$i]; | |
$cellData =~ s/↵/\n/g; | |
$cellData =~ s/⇥/\t/g; | |
print "\t\"$header[$i]\": "; | |
# check for data types | |
if($cellData eq "NULL") { | |
print "null"; | |
} | |
elsif($meta[$i]->[1] eq "integer" || $meta[$i]->[1] eq "float") { | |
chomp($cellData); | |
$d = $cellData+0; | |
print "$d"; | |
} else { | |
chomp($cellData); | |
print "\"$cellData\""; | |
} | |
# suppress last , | |
if($i<$h_cnt) { | |
print ","; | |
} | |
print "\n"; | |
} | |
print "}"; | |
# get next row | |
$rowData=<>; | |
# suppress last , | |
if($rowData) { | |
print ","; | |
} | |
print "\n"; | |
} | |
' | __CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbcopy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment