2 ms·
JSON Reformat and Print
sed 's/.\{4\}//' <<eof|flex -8iCrf
/* 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
%%
\40+
/* quoted values
"key":"
"key": "
*/
\"[^\":\]\)]*\":\40*\" echo;jmp x1;
/* quoted values
"key":[{
"key": [{
*/
\"[^\":\]\)]*\":\40*\[\40*\{ echo;pc(10);
/* unquoted, comma-separated array values
"key":[
"key": [
*/
\"[^\":\]\)]*\":\40*\[ echo;jmp x1;
/* quoted values
"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
This is made according to own personal JSON readability aesthetics, primarily for reading YouTube JSON. It is used in personal YouTube search utility. JSON from non-YouTube sources, e.g., Twitter, may contain escaped quotes. Remove esacped quotes first, e.g.,
yy064 < 1.json|yy059
Here is yy064:
sed 's/.\{4\}//' <<eof|flex -8iCrf
/* json remove escaped quotes */
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)
int setenv (const char *, const char *, int);
%option nounput noinput noyywrap
%%
^[\40\11]+
\134\42
%%
int main(){ yylex();exit(0);}
eof
cc -std=c89 -Wall -pedantic -pipe lex.yy.c -static -o yy064