User Tools

Site Tools


mmdblookup

mmdblookup

Fix GeoIP's mmdblookup output to make it real json:

mmdblookup -f /var/lib/GeoIP/GeoLite2-City.mmdb -i $ip \
| perl -0777 -pe 's/\s+<\w+>$/, /gm; s/([\d"])$/$1,/s; s/\s+/ /g; s/,\s*(})/$1/g; s/}\s*([\d"])/}, $1/g;' \
| jq

The regexes are (this can certainly be simplified!) :

s/\s+<\w+>$/, /gm; # remove type indicator
s/([\d"])$/$1,/s;  # add comma after value
s/\s+/ /g;         # make it all one line
s/,\s*(})/$1/g;    # remove extra commas
s/}\s*([\d"])/}, $1/g; # add missing commas

See also

/docs/dokuwiki/data/pages/mmdblookup.txt · Last modified: 2019-10-27 15:10:19 by mi