Skip to content

Refactor json parsing using loop + associative array #12

Description

@Costinteo

The json parsing seen below is not the most adequate in regards to good practices:

ipfetch/ipfetch

Lines 17 to 26 in 1e2bf53

ip=`sed -nr 's/.+"ip":"([0-9\.]+).+/\1/p' "$tempfile"`
continent=`sed -nr 's/.+"continent_code":"([^"]*).+/\1/p' "$tempfile"`
city=`sed -nr 's/.+"city":"([^"]*).+/\1/p' "$tempfile"`
isp=`sed -nr 's/.+"organization":"([^"]*).+/\1/p' "$tempfile"`
country=`sed -nr 's/.+"country":"([^"]*).+/\1/p' "$tempfile"`
timezone=`sed -nr 's/.+"timezone":"([^"]*).+/\1/p' "$tempfile"`
latitude=`sed -nr 's/.+"latitude":([^,]*).+/\1/p' "$tempfile"`
longitude=`sed -nr 's/.+"longitude":([^,]*).+/\1/p' "$tempfile"`
asn=`sed -nr 's/.+"asn":([^,]*).+/\1/p' "$tempfile"`
region=`sed -nr 's/.+"region":"([^"]*).+/\1/p' "$tempfile"`

It should probably be refactored using associative arrays (dictionaries) and a loop. There are some minor tricks to this - the regex pattern isn't always the same - but it could be done without much hassle. I propose this to anyone who wants to contribute. :)

@trakBan, would be great if you could label this with "Suggestion" or something like that. + "Help wanted" or "Good first issue".

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions