Accessing USGS Water data using urlfetch & JSON

Asked Jul 5, 2022·6 replies·Last activity 4 years ago·Open in Discord ↗
Archived from the original community forum
Things may have changed since it was written. Still stuck? Ask the community on Discord.

I am attempting to read the USGS water gauges to display the gauge height a specific gauge.

Here is the JSON I have no clue how to parse since I'm not a programmer by any means.

https://waterservices.usgs.gov/nwis/iv/?sites=01524500&parameterCd=00065&site…

the bit I need is "variableName":"Gage height, ft",

and "values":[{"value":[{"value":"0.57"

Any help extracting this information into a readable format and a command my viewers can use would be greatly appreciated.

Thank you!

6 replies

Hey @Tranquil Goat!

It couldn’t be easier! With JSON you move up the keys tree using dots.

With that said, this is how you get your data:

$(eval const api = $(urlfetch json https://waterservices.usgs.gov/nwis/iv/?sites=01524500&parameterCd=00065&format=json); `Gage height (ft): ${api.value.timeSeries[0].values[0].value[0].value}`)

Or with the second link:

$(eval const api = $(urlfetch json https://waterwatch.usgs.gov/webservices/realtime?site=01524500&format=json); `Gage height (ft): ${api.sites[0].stage}`)

The first command is too long, it has to fit into 200 characters. And the second command throws an error "
​!depthtest

11:22 AM
Nightbot
​Cannot access 'api' before initialization

I'm not sure what else to do except to try something else entirely, not sure what though.

I appreciate your help @Polar Cat ! 🙂

For the first command, I'm doing with what you gave me, after looking at the response, we can save a bit of space by removing useless parts of the query in the URL, that should fit. You have to specify if you're on YouTube, I can't guess, we assume Twitch by default, where the limit is higher.

For the second command, you're right, that's a typo, I forgot a semicolon.

Both code are updated in my first response.

Oh okay, I don't know anything about Twitch, I'm using Youtube. Sorry about that miscommunication.

Thank you again for your help. I'll test it out.

Polar Cat said:
$(eval const api = $(urlfetch json https://waterwatch.usgs.gov/webservices/realtime?site=01524500&format=json); Gage height (ft): ${api.sites[0].stage})

IT WORKED! You are everyone's savior! Even the USGS programmers will be happy to know someone got it working! Alright! 💯 💯

screenshot.png

Didn't find your answer? The community is on Discord.

Ask on Discord