It is currently April 25th, 2024, 1:39 am

Regex for JSON

Get help with creating, editing & fixing problems with skins
ms310
Posts: 225
Joined: April 1st, 2015, 7:16 am

Regex for JSON

Post by ms310 »

Hello experts!

I have found a time/timezone feed that I really like. The return is simple, but I am struggling to get a match for the value I am looking for.

The example is this: http://worldtimeapi.org/api/timezone/Asia/Tokyo

Code: Select all

{"week_number":38,"utc_offset":"+09:00","utc_datetime":"2019-09-21T04:00:23.191354+00:00","unixtime":1569038423,"timezone":"Asia/Tokyo","raw_offset":32400,"dst_until":null,"dst_offset":0,"dst_from":null,"dst":false,"day_of_year":264,"day_of_week":6,"datetime":"2019-09-21T13:00:23.191354+09:00","client_ip":"121.107.71.135","abbreviation":"JST"}
I am looking to capture:

utc_offset, timezone, dst, datetime and abbreviation

I realize this should be simple but I am really stumbling as there is a "utc_datetime" entry which I do not want. Now my head is wrapped around the axle and I am capturing the entire string.

Help appreciated!
Last edited by ms310 on September 21st, 2019, 4:11 am, edited 1 time in total.
ms310
Posts: 225
Joined: April 1st, 2015, 7:16 am

Re: Regex for JSON

Post by ms310 »

Nevermind!

(?siU).*\"utc_offset\":\"(.*)\".*.*\"timezone\":\"(.*)\".*.*\"datetime\":\".*T(.*)\..*\".*\"abbreviation\":\"(.*)\".*

1 => +09:00
2 => Asia/Tokyo
3 => 12:38:08
4 => JST

This gets me what I want. Man, I scratched my head for a while! I adjusted how to match the time to get rid of the stuff I didn't need.

Hope this helps someone in the future!
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5406
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Regex for JSON

Post by eclectic-tech »

Thanks for the info about the WorldTime API 8-)
Glad to see you figured out the RegExp too! :thumbup:
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Regex for JSON

Post by balala »

ms310 wrote: September 21st, 2019, 4:05 am The example is this: http://worldtimeapi.org/api/timezone/Asia/Tokyo
Interesting example, however I can't get it to work for me. I mean that if I replace the Asia and Tokyo with anything related to Europe, there is no json file to download.
Is there a site to look for the appropriate URL for other locations around the world? :confused:
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5406
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Regex for JSON

Post by eclectic-tech »

balala wrote: September 21st, 2019, 6:32 pm Interesting example, however I can't get it to work for me. I mean that if I replace the Asia and Tokyo with anything related to Europe, there is no json file to download.
Is there a site to look for the appropriate URL for other locations around the world? :confused:
Have a look at this information page: http://worldtimeapi.org/
It details how the API should work. I haven't tried to use in any skins, but it looks like it may replace the Yandex time site I currently use...
ms310
Posts: 225
Joined: April 1st, 2015, 7:16 am

Re: Regex for JSON

Post by ms310 »

eclectic-tech wrote: September 21st, 2019, 9:07 pm Have a look at this information page: http://worldtimeapi.org/
It details how the API should work. I haven't tried to use in any skins, but it looks like it may replace the Yandex time site I currently use...
This is what I was thinking too!

The disadvantage I see vs. Yandex is you can only get one result at a time. Your solution for Yandex is quite cool - you fetch all zones with one call. With the Worldtimeapi.org feed I am thinking I need to make 6 separate calls:

Code: Select all

Loc1 = http://worldtimeapi.org/timezone/America/Detroit
Loc2 = http://worldtimeapi.org/timezone/America/Chicago
Loc3 = http://worldtimeapi.org/timezone/America/Los_Angeles
Loc4 = http://worldtimeapi.org/timezone/Pacific/Honolulu
Loc5 = http://worldtimeapi.org/timezone/Europe/London
Loc6 = http://worldtimeapi.org/timezone/Indian/Chagos
Any ideas to make this feed give us all values with one call?
ms310
Posts: 225
Joined: April 1st, 2015, 7:16 am

Re: Regex for JSON

Post by ms310 »

I've used a method Eclectic-Tech used in an older TimeZone skin - I am calling WebParser.dll 6 times by starting with them all disabled and only enabling each one when the prior call finishes.

The result is a little slow and sometimes the first call finishes last - but - so far I think it is working. Since the Worldtimeapi returns the utc_offset directly and the DST true/false the date functions in Rainmeter seems to handle the rest.

Thanks as always for the help!

The current result is shown below. I may add some formatting and coloring but so far I am OK with the progress.
2019-09-22_20h25_01.png
You do not have the required permissions to view the files attached to this post.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5406
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Regex for JSON

Post by eclectic-tech »

@ms310 Yes, I have done the access both in 1 call and as separate calls.

F.Y.I.
I did this because Yandex returns the values according to their numbering system when using 1 call. This made it difficult to easily arrange the information in an alternate order.

My solution was to create separate variables for each returned city/time, and assign them to the desired measures which let's you display the cities in any desired order.

Code: Select all

[Variables]
; ------------------------------------------------------------------------
; SORT CITIES DISPLAY ORDER
; ------------------------------------------------------------------------
; To sort times manually change the value after the equal sign ending number (1~8) based city name tooltip info in the skin (hover over city name to see the # for that city) 
City1=measureName4
Time1=measureTime4
City2=measureName2
Time2=measureTime2
City3=measureName5
Time3=measureTime5
City4=measureName6
Time4=measureTime6
City5=measureName3
Time5=measureTime3
City6=measureName1
Time6=measureTime1
City7=measureName7
Time7=measureTime7
City8=measureName8
Time8=measureTime8
Then in the meters would be

Code: Select all

; ------------------------------------------------------------------------
; World Time Meters
; ------------------------------------------------------------------------

[meterZoneName1]
Meter=String
MeterStyle=StyleSmallText
MeasureName=#City1#
X=#LeftPadding#
Y=([Date:Y]+30)
W=(#BackgroundWidth#*0.5)
Group=Times
UpdateDivider=-1
Tooltiptext=#City1#

[meterTime1]
Meter=String
MeterStyle=StyleSmallText | StyleRightAlign
MeasureName=#Time1#
X=(#BackgroundWidth#-#LeftPadding#)
Y=r
W=(#BackgroundWidth#*0.5)
Text=%1
; %1 stands for the value of MeasureName (measureTime in this case).
Group=Times

[meterZoneName2]
Meter=String
MeterStyle=StyleSmallText
MeasureName=#City2#
;MeasureName2=measureTime2Zone
X=#LeftPadding#
Y=4R
W=(#BackgroundWidth#*0.5)
Group=Times
UpdateDivider=-1
Tooltiptext=#City2#

[meterTime2]
Meter=String
MeterStyle=StyleSmallText | StyleRightAlign
MeasureName=#Time2#
X=(#BackgroundWidth#-#LeftPadding#)
Y=r
W=(#BackgroundWidth#*0.5)
Text=%1
; %1 stands for the value of MeasureName (measureTime in this case).
Group=Times
...
As usual there are many ways to achieve the desired result. I doubt if multiple calls can be combined using this API. :uhuh:
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Regex for JSON

Post by balala »

eclectic-tech wrote: September 21st, 2019, 9:07 pm Have a look at this information page: http://worldtimeapi.org/
Right, I got it. Thanks for the info.
ms310
Posts: 225
Joined: April 1st, 2015, 7:16 am

Re: Regex for JSON

Post by ms310 »

eclectic-tech wrote: September 22nd, 2019, 3:25 pm @ms310 Yes, I have done the access both in 1 call and as separate calls.

F.Y.I.
I did this because Yandex returns the values according to their numbering system when using 1 call. This made it difficult to easily arrange the information in an alternate order.

My solution was to create separate variables for each returned city/time, and assign them to the desired measures which let's you display the cities in any desired order.

Code: Select all

[Variables]
; ------------------------------------------------------------------------
; SORT CITIES DISPLAY ORDER
; ------------------------------------------------------------------------
; To sort times manually change the value after the equal sign ending number (1~8) based city name tooltip info in the skin (hover over city name to see the # for that city) 
City1=measureName4
Time1=measureTime4
City2=measureName2
Time2=measureTime2
City3=measureName5
Time3=measureTime5
City4=measureName6
Time4=measureTime6
City5=measureName3
Time5=measureTime3
City6=measureName1
Time6=measureTime1
City7=measureName7
Time7=measureTime7
City8=measureName8
Time8=measureTime8
Then in the meters would be

Code: Select all

; ------------------------------------------------------------------------
; World Time Meters
; ------------------------------------------------------------------------

[meterZoneName1]
Meter=String
MeterStyle=StyleSmallText
MeasureName=#City1#
X=#LeftPadding#
Y=([Date:Y]+30)
W=(#BackgroundWidth#*0.5)
Group=Times
UpdateDivider=-1
Tooltiptext=#City1#

[meterTime1]
Meter=String
MeterStyle=StyleSmallText | StyleRightAlign
MeasureName=#Time1#
X=(#BackgroundWidth#-#LeftPadding#)
Y=r
W=(#BackgroundWidth#*0.5)
Text=%1
; %1 stands for the value of MeasureName (measureTime in this case).
Group=Times

[meterZoneName2]
Meter=String
MeterStyle=StyleSmallText
MeasureName=#City2#
;MeasureName2=measureTime2Zone
X=#LeftPadding#
Y=4R
W=(#BackgroundWidth#*0.5)
Group=Times
UpdateDivider=-1
Tooltiptext=#City2#

[meterTime2]
Meter=String
MeterStyle=StyleSmallText | StyleRightAlign
MeasureName=#Time2#
X=(#BackgroundWidth#-#LeftPadding#)
Y=r
W=(#BackgroundWidth#*0.5)
Text=%1
; %1 stands for the value of MeasureName (measureTime in this case).
Group=Times
...
As usual there are many ways to achieve the desired result. I doubt if multiple calls can be combined using this API. :uhuh:
Yeah - I couldn’t figure out a way to get the worldtimeapi site to return multiple values - I am thankful you created the multiple calls method that I could hack apart. Thank you!