Accessing USGS Water data using urlfetch & JSON

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&siteStatus=all&format=json

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!

Okay with the help of the USGS Water Service programmers I found this:
https://waterwatch.usgs.gov/webservices/realtime?site=01524500&format=csv

How do I get the value I need out of that?

Can anyone please help? I am not a programmer, I just want this gauge height measurement.

Hey @onefish!

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}`)
1 Like

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 @Emily ! :slight_smile:

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.

1 Like

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.

1 Like

IT WORKED! You are everyone’s savior! Even the USGS programmers will be happy to know someone got it working! Alright! :100: :100:
screenshot

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.