3 ms·
JSON Extract, Reformat and Print
Improved slightly over https://news.ycombinator.com/item?id=30916201
sed 's/.\{4\}//' <<eof|flex -8iCrf
/* extract, reformat, print json */
int fileno (FILE *);
#define echo do{if(fwrite(yytext,(size_t)yyleng,1,yyout)){}}while(0)
#define jmp (yy_start) = 1 + 2 *
#define pc(x) putchar(x)
%option nounput noinput noyywrap
%s x1 x2
%%
^\40+
/* quotes
"key":"
"key": "
*/
\"[^\":\]\)]*\":\40*\" echo;jmp x1;
/* bracket followed by brace
"key":[{
"key": [{
*/
\"[^\":\]\)]*\":\40*\[\40*\{ echo;pc(10);
/* number
"key":1
"key": 1
*/
\"[^\":\]\)]*\":\40*[0-9\40\12\15]* echo;jmp x2;
<x2>[,}] pc(10);jmp 0;
/* bracket (unquoted, comma-separated array values)
"key":[
"key": [
*/
\"[^\":\]\)]*\":\40*\[ echo;jmp x1;
/* brace
"key":{
"key": {
*/
\"[^\":\]\)]*\":\40*\{ echo;pc(10);
/* open brace not closed brace */
<x1>[\]\{\"] echo;pc(10);jmp 0;
<x1>[^\]\{\"\12]* echo;
\n|.
%%
int main(){ yylex();exit(0);}
eof
cc -std=c89 -Wall -pedantic -pipe lex.yy.c -static -o yy059
Usage: yy059 < 1.json
yy059 is for extracting JSON from HTML and printing it as a new, left-justified, line-delimited format readable to me.
To use with indented JSON, e.g., json_reformat output (json_reformat from yajl project), first remove the indentation, e.g.,
sed 's/ *//' 1.json | yy059
NB. This is made for me, primarily for reading YouTube JSON. It is used in a personal YouTube search utility. JSON from non-YouTube sources, e.g., Twitter, may contain escaped quotes, often for including quotes associated with href tags. Remove escaped quotes first, e.g., using yy064, disclosed here: https://news.ycombinator.com/item?id=30916201
- enes66 5y ago
- enes66 5y ago
- enes66 5y ago
- yuppie_scum 5y agoHave you used jq?
- 1vuio0pswjnm7 5y agoCorrected, based on some new test JSON^1 Print state transitions to standard error Usage: yy059 < 1.json 2>1.dbg | less less 1.dbg /* extract, reformat, print json */ int fileno (FILE *); #define echo do{if(fwrite(yytext,(size_t)yyleng,1,yyout)){}}while(0) #define jmp (yy_start) = 1 + 2 * #define pc(x) putchar(x) #define dbg(x) fprintf(stderr,"%s\t%s\n",x,yytext); %option nounput noinput noyywrap %s x1 x2 x3 x4 %% ^\40+ /* quotes +-------------------+ | "key":" | | "key": " | +-------------------+ */ \"[^\":\]\)]*\":\40*\" { dbg("0--x1"); echo; jmp x1;} /* bracket + brace +-------------------+ | "key":[{ | | "key": [{ | +-------------------+ */ \"[^\":\]\)]*\":\40*\[\40*\{ { dbg("0--0"); echo;pc(10);} /* number +-------------------+ |"key":1 | |"key": 1 | +-------------------+ */ \"[^\":\]\)]*\":\40*[0-9\40\12\15]* { dbg("0--x2"); echo; jmp x2;} /* bracket +-------------------+ | "key":[ | | "key": [ | | "key":[ | | "value" | | "key": [ | | "value" | | "key":[ | | "[value]" | | "key": [ | | "[value]" | +-------------------+ */ \"[^\":\]\)]*\":\40*\[ { dbg("0--x3"); echo; jmp x3;} /* brace +-------------------+ | "key":{ | | "key": { | +-------------------+ */ \"[^\":\]\)]*\":\40*\{ { echo;pc(10);} /* NB. [\{] open brace */ <x1>[\]\{\"] { dbg("x1--0"); echo;pc(10); jmp 0;} <x1>[^\]\{\"\12\40]* { echo;} <x2>[,}] { dbg("x2--0"); pc(10); jmp 0;} <x3>[\]\"] { dbg("x3--0"); echo;pc(10); jmp 0;} <x3>[^\{\12\40\]]* echo; \n|. %% int main(){ yylex();exit(0);} 1. https://gist.githubusercontent.com/wololock/0ba478cfeb73b36c41c4f8c814111db3/raw/4dd7d45b7c4a6c374a8233407be4f39ede7e420c/openlibrary.json https://gist.githubusercontent.com/wololock/0ba478cfeb73b36c... Need more JSON samples to test.
- deleted 5y ago[deleted]
- 1vuio0pswjnm7 5y agoFixed, corrected, improved. /* extract, reformat, print json */ int dprintf(int, const char *__restrict, ...); int fileno (FILE *); #define echo do{if(fwrite(yytext,(size_t)yyleng,1,yyout)){}}while(0) #define jmp (yy_start) = 1 + 2 * #define pc(x) putchar(x) #define dbg(x) dprintf(3,"%s\t%s\n",x,yytext); %option nounput noinput noyywrap %s brace bracket bracketbrace number quotes %% /* brace +-------------------+ | "key":{ | | "key": { | +-------------------+ */ \"[^\":\]\)=]*\":\40*\{ { dbg("<<brace>>"); pc(10); echo; jmp brace;} <brace>\} echo;jmp 0; <brace>. echo; /* bracket +-------------------+ | "key":[ | | "key": [ | | "key":[ | | "value" | | "key": [ | | "value" | | "key":[ | | "[value]" | | "key": [ | | "[value]" | +-------------------+ */ \"[^\":\]\)=]*\":\40*\[ { dbg("<<bracket>>"); pc(10); echo; jmp bracket;} <bracket>\] echo;jmp 0; <bracket>. echo; /* bracket + brace +-------------------+ | "key":[{ | | "key": [{ | +-------------------+ */ \"[^\":\]\)=]*\":\40*\[\40*\{ { dbg("<<bracket+brace>>"); pc(10); echo; jmp bracketbrace;} <bracketbrace>\] echo;jmp 0; <bracketbrace>. echo; /* number +-------------------+ |"key":1 | |"key": 1 | +-------------------+ */ \"[^\":\]\)=]*\":\40*[0-9\40\12\15]* { dbg("<<number>>"); pc(10); echo; jmp number;} <number>, jmp 0; <number>. echo; /* quotes +-------------------+ | "key":" | | "key": " | +-------------------+ */ \"[^\":\]\)=]*\":\40*\" { dbg("<<quotes>>"); pc(10); echo; jmp quotes;} <quotes>\" echo;jmp 0; <quotes>. echo; \n|. %% int main(){ yylex();exit(0);}
- 1vuio0pswjnm7 5y agoImproved. /* extract, reformat, print json */ int dprintf(int, const char *__restrict, ...); int fileno (FILE *); int x,y,z; #define echo do{if(fwrite(yytext,(size_t)yyleng,1,yyout)){}}while(0) #define jmp (yy_start) = 1 + 2 * #define pc(x) putchar(x) #define dbg(x) dprintf(3,"%s\t%s\n",x,yytext); %option nounput noinput noyywrap %s boolean brace bracket bracketbrace number quotes %% \40\40+ \11+ \12 /* boolean +-------------------+ | "key":true | | "key": true | +-------------------+ */ \"[^\":\]\)=]*\":\40*"(true)|(false)" { dbg("<<boolean>>"); pc(10); echo; jmp boolean;} <boolean>[e\}] pc(101);jmp 0; <boolean>[^\}] echo; /* brace +-------------------+ | "key":{ | | "key": { | +-------------------+ */ \"[^\":\]\)=]*\":\40*\{ { dbg("<<brace>>"); pc(10); echo; jmp brace;} <brace>\} jmp 0; <brace>[^\}] echo; /* bracket +-------------------+ | "key":[ | | "key": [ | | "key":[ | | "value" | | "key": [ | | "value" | | "key":[ | | "[value]" | | "key": [ | | "[value]" | +-------------------+ */ \"[^\":\]\)=]*\":\40*\[ { dbg("<<bracket>>"); pc(10); echo; jmp bracket;} <bracket>\] echo;jmp 0; <bracket>[^\]] echo; /* bracket + brace +-------------------+ | "key":[{ | | "key": [{ | +-------------------+ */ \"[^\":\]\)=]*\":\40*\[\40*\{ { dbg("<<bracket+brace>>"); pc(10); echo; jmp bracketbrace;} <bracketbrace>\] jmp 0; <bracketbrace>[^\]\}] echo; /* number +-------------------+ |"key":1 | |"key": 1 | +-------------------+ */ \"[^\":\]\)=]*\":\40*[0-9\40\12\15]* { dbg("<<number>>"); pc(10); echo; jmp number;} <number>, jmp 0; <number>[^,\}\]] echo; /* quotes +-------------------+ | "key":" | | "key": " | +-------------------+ */ \"[^\":\]\)=]*\":\40*\" { dbg("<<quotes>>"); pc(10); echo; jmp quotes;} <quotes>\" echo;jmp 0; <quotes>[^\"] echo; \n|. %% int main(){ yylex();pc(10);exit(0);}