It is currently October 17th, 2021, 3:43 pm

parsing data from JSON file

Help with creating, editing & fixing problems with skins
Kea
Posts: 7
Joined: October 1st, 2021, 9:10 pm

parsing data from JSON file

Post by Kea »

I'm to parse a lot JSON data from a local file into rainmeter - how would I go about doing that?
There's a lot of data to parse and there are multiple values with the same names in the file (example below)

Code: Select all

{
  id: 732282,
  date: 20211005,
  startTime: 1025,
  endTime: 1110,
  kl: [
    {
      id: 650,
      name: '1BK1T1',
      longname: 'Technisches Berufskolleg 1.J, Klasse 1'
    }
  ],
  te: [ { id: 224, name: 'Hl', longname: 'Hillger' } ],
  su: [ { id: 67, name: 'MT', longname: 'Medientechnik (TBK1)' } ],
  ro: [ { id: 144, name: 'A3.141', longname: 'Unterrichtsraum' } ],
  lsnumber: 14800,
  activityType: 'Unterricht'
}
{
  id: 822772,
  date: 20211005,
  startTime: 835,
  endTime: 920,
  code: 'cancelled',
  kl: [
    {
      id: 650,
      name: '1BK1T1',
      longname: 'Technisches Berufskolleg 1.J, Klasse 1'
    }
  ],
  te: [ { id: 72, name: 'Wp', longname: 'Wegkamp' } ],
  su: [
    {
      id: 280,
      name: 'ATK',
      longname: 'Angewandte Technik (TBK) - Konstruktion'
    }
  ],
  ro: [ { id: 144, name: 'A3.141', longname: 'Unterrichtsraum' } ],
  lsnumber: 102600,
  activityType: 'Unterricht'
}

I need to get keys like "name" multiple times with diffrent values, is that possible?
I'm fine with doing it with the help of lua, if it's impossible I could seperate everything between the { } in different
files before exporting in the nodeJS file that gets these values but I would prefer if it only requires a single file.
User avatar
balala
Rainmeter Sage
Posts: 13319
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: parsing data from JSON file

Post by balala »

Kea wrote: October 5th, 2021, 3:54 pm I'm to parse a lot JSON data from a local file into rainmeter - how would I go about doing that?
There's a lot of data to parse and there are multiple values with the same names in the file (example below)
Am I correct when thinking the number of those elements which have to be get varies?
Kea
Posts: 7
Joined: October 1st, 2021, 9:10 pm

Re: parsing data from JSON file

Post by Kea »

balala wrote: October 5th, 2021, 4:13 pm Am I correct when thinking the number of those elements which have to be get varies?
sadly, yes
User avatar
balala
Rainmeter Sage
Posts: 13319
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: parsing data from JSON file

Post by balala »

Kea wrote: October 5th, 2021, 4:24 pmsadly, yes
No, this is not a problem. In such cases Lookahead Assertion might help. Does this time as well, I think:

Code: Select all

[Rainmeter]
DynamicWindowSize=1

[Variables]
Item=(?(?=.*, name:).*'(.{1,10})',)

[MeasureName]
Measure=WebParser
URL=file://PATH-OF-JSON-FILE
RegExp=(?siU)name: '(.*)'#Item#.*#Item#.*#Item#.*#Item#.*#Item#.*#Item#.*#Item#.*#Item#.*#Item#

[MeasureName1]
Measure=WebParser
Url=[MeasureName]
StringIndex=1

[MeasureName2]
Measure=WebParser
Url=[MeasureName]
StringIndex=2

[MeasureName3]
Measure=WebParser
Url=[MeasureName]
StringIndex=3

[MeasureName4]
Measure=WebParser
Url=[MeasureName]
StringIndex=4

[MeasureName5]
Measure=WebParser
Url=[MeasureName]
StringIndex=5

[MeasureName6]
Measure=WebParser
Url=[MeasureName]
StringIndex=6

[MeasureName7]
Measure=WebParser
Url=[MeasureName]
StringIndex=7

[MeasureName8]
Measure=WebParser
Url=[MeasureName]
StringIndex=8

[MeasureName9]
Measure=WebParser
Url=[MeasureName]
StringIndex=9

[MeasureName10]
Measure=WebParser
Url=[MeasureName]
StringIndex=10

[MeterNames]
Meter=STRING
MeasureName=MeasureName1
MeasureName2=MeasureName2
MeasureName3=MeasureName3
MeasureName4=MeasureName4
MeasureName5=MeasureName5
MeasureName6=MeasureName6
MeasureName7=MeasureName7
MeasureName8=MeasureName8
MeasureName9=MeasureName9
MeasureName10=MeasureName10
X=0
Y=0
Padding=15,5,15,5
FontColor=220,220,220
FontEffectColor=0,0,0
StringEffect=Shadow
SolidColor=0,0,0,150
FontSize=8
FontFace=Segoe UI
StringStyle=BOLD
StringAlign=LEFT
AntiAlias=1
Text=1. %1#CRLF#2. %2#CRLF#3. %3#CRLF#4. %4#CRLF#5. %5#CRLF#6. %6#CRLF#7. %7#CRLF#8. %8#CRLF#9. %9#CRLF#10. %10
Don't forget to add the path of the file to be parsed, into the URL option of the [MeasureName] measure.
The [MeasureName1] - [MeasureName10] measures are returning the names. If there is less names than 10, the first measures are returning the names, the following ones remain blank.
Obviously if needed, further .*#Item# elements can be added to the RegExp option of the [MeasureName] measure, along with the appropriate WebParser measures, after [MeasureName10].
Kea
Posts: 7
Joined: October 1st, 2021, 9:10 pm

Re: parsing data from JSON file

Post by Kea »

balala wrote: October 5th, 2021, 5:21 pm No, this is not a problem. In such cases Lookahead Assertion might help. Does this time as well, I think:

Code: Select all

[Rainmeter]
DynamicWindowSize=1

[Variables]
Item=(?(?=.*, name:).*'(.{1,10})',)

[MeasureName]
Measure=WebParser
URL=file://PATH-OF-JSON-FILE
RegExp=(?siU)name: '(.*)'#Item#.*#Item#.*#Item#.*#Item#.*#Item#.*#Item#.*#Item#.*#Item#.*#Item#

[MeasureName1]
Measure=WebParser
Url=[MeasureName]
StringIndex=1

[MeasureName2]
Measure=WebParser
Url=[MeasureName]
StringIndex=2

[MeasureName3]
Measure=WebParser
Url=[MeasureName]
StringIndex=3

[MeasureName4]
Measure=WebParser
Url=[MeasureName]
StringIndex=4

[MeasureName5]
Measure=WebParser
Url=[MeasureName]
StringIndex=5

[MeasureName6]
Measure=WebParser
Url=[MeasureName]
StringIndex=6

[MeasureName7]
Measure=WebParser
Url=[MeasureName]
StringIndex=7

[MeasureName8]
Measure=WebParser
Url=[MeasureName]
StringIndex=8

[MeasureName9]
Measure=WebParser
Url=[MeasureName]
StringIndex=9

[MeasureName10]
Measure=WebParser
Url=[MeasureName]
StringIndex=10

[MeterNames]
Meter=STRING
MeasureName=MeasureName1
MeasureName2=MeasureName2
MeasureName3=MeasureName3
MeasureName4=MeasureName4
MeasureName5=MeasureName5
MeasureName6=MeasureName6
MeasureName7=MeasureName7
MeasureName8=MeasureName8
MeasureName9=MeasureName9
MeasureName10=MeasureName10
X=0
Y=0
Padding=15,5,15,5
FontColor=220,220,220
FontEffectColor=0,0,0
StringEffect=Shadow
SolidColor=0,0,0,150
FontSize=8
FontFace=Segoe UI
StringStyle=BOLD
StringAlign=LEFT
AntiAlias=1
Text=1. %1#CRLF#2. %2#CRLF#3. %3#CRLF#4. %4#CRLF#5. %5#CRLF#6. %6#CRLF#7. %7#CRLF#8. %8#CRLF#9. %9#CRLF#10. %10
Don't forget to add the path of the file to be parsed, into the URL option of the [MeasureName] measure.
The [MeasureName1] - [MeasureName10] measures are returning the names. If there is less names than 10, the first measures are returning the names, the following ones remain blank.
Obviously if needed, further .*#Item# elements can be added to the RegExp option of the [MeasureName] measure, along with the appropriate WebParser measures, after [MeasureName10].
Thank you for all your help! Gonna try to get all that working in my script now.
User avatar
balala
Rainmeter Sage
Posts: 13319
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: parsing data from JSON file

Post by balala »

Kea wrote: October 5th, 2021, 5:48 pm Thank you for all your help! Gonna try to get all that working in my script now.
Alright, please let me know if it is.