how read json  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
OedzesG
Posts: 106
Joined: Monday 11 March 2019 0:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

how read json

Post by OedzesG »

Hallo,

i try to get Tautulli data to Domoticz but having some problems with reading.

Can someone tell me how i read this json call?
Spoiler: show

Code: Select all




{
  "response": {
    "message": null,
    "data": {
      "sessions": [
        {
          "rating": "8.0",
          "transcode_width": "",
          "labels": [
            
          ],
          "stream_video_color_primaries": "bt709",			
          "stream_bitrate": "4934",
          "bandwidth": "5181",
          "optimized_version": 0,				
          "video_language": "",
          "parent_rating_key": "50",
          "rating_key": "62",
          "video_decision": "transcode",
          "transcode_hw_decoding": 1,
          "thumb": "/library/metadata/62/thumb/1583237929",
          "title": "Pilot",
          "video_codec_level": "40",
          "tagline": "",
          "last_viewed_at": "1583269297",
          "synced_version": 0,
          "transcode_audio_channels": "2",
          "user_rating": "",
          "platform": "Chrome",
          "collections": [
            
          ],
          "location": "lan",
          "transcode_container": "mp4",
          "audio_channel_layout": "5.1(side)",
          "stream_duration": "4874976",
          "local": 1,
          "stream_video_ref_frames": "",
          "stream_video_width": "1920",
          "transcode_hw_encode_title": "Intel (VA API)",
          "stream_container_decision": "transcode",
          "audience_rating": "",
          "full_title": "Suits - Pilot",
          "ip_address": "192.168.1.14",
          "transcode_speed": "0.0",
          "stream_subtitle_language": "Nederlands",
          "channel_stream": 0,
          "video_bitrate": "3119",
          "is_allow_sync": 1,
          "stream_video_bitrate": "4678",
          "summary": "Harvey geeft Mike zijn eerste zaak, een pro-bono zaak waarbij een CEO wordt verdacht van seksuele intimidatie. Harvey moet zijn beste spel uit de kast trekken, om Mike gemotiveerd te houden.",
          "stream_video_color_range": "",
          "stream_audio_decision": "transcode",
          "aspect_ratio": "1.78",
          "audio_bitrate_mode": "",
          "transcode_hw_decode_title": "Intel (VA API)",
          "machine_id": "e8z5wabv80hjz27c5jylzghi",
          "deleted_user": 0,
          "library_name": "Series",
          "art": "/library/metadata/62/art/1583237929",
          "stream_video_resolution": "1080",
          "video_profile": "high",
          "video_color_space": "bt709",
          "sort_title": "",
          "stream_video_codec_level": "",
          "stream_video_height": "1080",
          "year": "2011",
          "duration": "4874976",
          "video_scan_type": "progressive",
          "progress_percent": "5",
          "video_language_code": "",
          "transcode_key": "/transcode/sessions/lofnrw2wr5qaz6emp72kbslb",
          "transcode_throttled": 1,
          "container": "mkv",
          "stream_audio_bitrate": "256",
          "user": "Gerard Oedzes",
          "selected": 1,
          "subtitle_location": "embedded",
          "transcode_hw_requested": 1,
          "video_height": "1080",
          "state": "paused",
          "is_restricted": 0,
          "email": "[email protected]",
          "stream_container": "mp4",
          "stream_video_dynamic_range": "SDR",
          "video_bit_depth": "8",
          "stream_audio_sample_rate": "",
          "grandparent_title": "Suits",
          "studio": "USA Network",
          "transcode_decision": "transcode",
          "video_width": "1920",
          "bitrate": "3503",
          "stream_audio_channel_layout": "Stereo",
          "originally_available_at": "2011-06-23",
          "video_frame_rate": "23.976",
          "synced_version_profile": "",
          "friendly_name": "Gerard Oedzes",
          "audio_profile": "",
          "video_chroma_subsampling": "4:2:0",
          "platform_name": "chrome",
          "stream_video_language": "",
          "keep_history": 1,
          "stream_subtitle_container": "",
          "stream_audio_codec": "aac",
          "stream_video_full_resolution": "1080p",
          "video_codec": "h264",
          "grandparent_thumb": "/library/metadata/49/thumb/1583237992",
          "audio_bitrate": "384",
          "transcode_hw_decode": "vaapi",
          "secure": 1,
          "user_thumb": "https://plex.tv/users/9f5caff9a3cd56ab/avatar?c=1578427885",
          "video_color_primaries": "bt709",
          "height": "1080",
          "stream_subtitle_decision": "burn",
          "audio_codec": "ac3",
          "parent_title": "Season 1",
          "guid": "com.plexapp.agents.thetvdb://247808/1/1?lang=nl",
          "stream_video_scan_type": "",
          "audio_language_code": "eng",
          "transcode_video_codec": "h264",
          "transcode_audio_codec": "aac",
          "stream_video_decision": "transcode",
          "stream_video_color_trc": "bt709",
          "user_id": 20020070,
          "transcode_height": "",
          "transcode_hw_full_pipeline": 0,
          "throttled": "1",
          "grandparent_guid": "com.plexapp.agents.thetvdb://247808?lang=nl",
          "live": 0,
          "width": "1920",
          "quality_profile": "Original",
          "stream_subtitle_forced": 0,
          "media_type": "episode",
          "video_resolution": "1080",
          "stream_subtitle_location": "segments-video",
          "do_notify": 1,
          "video_ref_frames": "4",
          "stream_subtitle_language_code": "dut",
          "product_version": "4.22.2",
          "stream_audio_language_code": "eng",
          "optimized_version_profile": "",
          "rating_image": "",
          "is_home_user": 0,
          "is_admin": 1,
          "ip_address_public": "213.93.65.245",
          "allow_guest": 0,
          "video_color_range": "tv",
          "audio_sample_rate": "48000",
          "stream_audio_channel_layout_": "",
          "media_index": "1",
          "stream_video_framerate": "24p",
          "transcode_hw_encode": "vaapi",
          "grandparent_rating_key": "49",
          "original_title": "",
          "added_at": "1583236273",
          "banner": "/library/metadata/49/banner/1583237992",
          "bif_thumb": "",
          "parent_media_index": "1",
          "live_uuid": "",
          "audio_language": "English",
          "stream_audio_bitrate_mode": "cbr",
          "username": "Gerard Oedzes",
          "video_color_trc": "bt709",
          "subtitle_decision": "burn",
          "parent_guid": "com.plexapp.agents.thetvdb://247808/1?lang=nl",
          "video_dynamic_range": "SDR",
          "children_count": "",
          "updated_at": "1583237929",
          "video_full_resolution": "1080p",
          "player": "Chrome",
          "audio_channels": "6",
          "subtitle_format": "",
          "file": "/home/gerard/synology_share/ds218/multimedia/serie/Suits/season 1/suits S01E01 Pilot.mkv",
          "file_size": "2134833392",
          "session_key": "12",
          "subtitles": 1,
          "id": "341",
          "subtitle_container": "",
          "optimized_version_title": "",
          "genres": [
            "Comedy",
            "Drama"
          ],
          "stream_audio_channels": "2",
          "stream_video_language_code": "",
          "relayed": 0,
          "indexes": 0,
          "platform_version": "80.0",
          "stream_audio_language": "English",
          "writers": [
            "Aaron Korsh"
          ],
          "actors": [
            "Aloma Wright",
            "Gabriel Macht",
            "Sarah Rafferty",
            "Wendell Pierce",
            "Patrick J. Adams",
            "Abigail Spencer",
            "Dulé Hill",
            "Gina Torres",
            "Amanda Schull",
            "D.B. Woodside",
            "Meghan Markle",
            "Rachael Harris",
            "Katherine Heigl",
            "Rick Hoffman"
          ],
          "stream_subtitle_format": "",
          "audio_decision": "transcode",
          "subtitle_forced": 0,							
          "profile": "Web",
          "product": "Plex Web",
          "view_offset": "238000",
          "type": "3",
          "audience_rating_image": "",
          "section_id": "3",
          "stream_subtitle_codec": "",
          "stream_video_color_space": "",
          "subtitle_codec": "ass",
          "stream_video_codec": "h264",
          "device": "Windows",
          "stream_video_bit_depth": "",
          "video_framerate": "24p",
          "transcode_hw_encoding": 1,
          "transcode_protocol": "dash",
          "shared_libraries": [
            "1",
            "2",
            "3"
          ],
          "stream_aspect_ratio": "",
          "content_rating": "TV-14",
          "session_id": "ppgvxw0s9rg4bq40ktm29gei",
          "directors": [
            "Kevin Bray"
          ],
          "parent_thumb": "/library/metadata/50/thumb/1583237889",
          "subtitle_language_code": "dut",
          "transcode_progress": 8,
          "subtitle_language": "Nederlands",
          "stream_video_chroma_subsampling": ""
        }
      ],
      "stream_count": "1",
      "total_bandwidth": 5181,
      "stream_count_transcode": 1,
      "wan_bandwidth": 0,
      "stream_count_direct_play": 0,
      "lan_bandwidth": 5181,
      "stream_count_direct_stream": 0
    },
    "result": "success"
  }
}


This is my script so far....

Code: Select all

		local scriptVersion = ' testing '
		local scriptVar = ' plex woonkamer ' .. scriptVersion

		local tautulli_ip 	= 'xxx'
		local tautulli_api 	= 'xxx'

	return 
		{
			on = 
		{
			customEvents	= { 'plex',},
		},
			httpResponses 	= { scriptVar, 
		}, 
			logging 		= { level	= domoticz.LOG_DEBUG, 
								marker 	= scriptVar,
		},

    execute = function(dz, item)
		
			dz.openURL	({ 
							url = 'http://' .. tautulli_ip .. '/api/v2?apikey=' .. tautulli_api .. '&cmd=get_activity', 
							method      = 'GET',
							callback    = scriptVar 
						}) 	
		
	
	    
    end
}
OedzesG
Posts: 106
Joined: Monday 11 March 2019 0:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: how read json

Post by OedzesG »

found that this gives me the data..

Code: Select all

	
		local tautulli_ip 	= 'xxx'
		local tautulli_api 	= 'xxx'

		return {
		on = {
	    customEvents	= { 'plex',},
		httpResponses = { 'plex' }
		},
        execute = function(dz, item)

		if (item.isCustomEvent) then
        dz.openURL({
        url = 'http://' .. tautulli_ip .. '/api/v2?apikey=' .. tautulli_api .. '&cmd=get_activity', 
        callback = 'plex'
        })
		end
    
      dz.log(item.data)
        
		end 
	} 

but i dont succeed in getting the state.. someone how can help me filtering the data?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how read json

Post by waaren »

OedzesG wrote: Wednesday 04 March 2020 16:48 i try to get Tautulli data to Domoticz but having some problems with reading.
Can someone tell me how i read this json call?
What I normally do to identify structure of json is to put complete JSON in a string and use dz.utils.dumpTable and dz.utils.fromJSON to create table and dump that table to log. It makes the reading of the JSON structure much easier,

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'JSONTrigger',  -- just an example to trigger the script
        },
    },

    execute = function(dz, item)

        local myJSONString = '{"response": {"message": null,"data": {"sessions": [{"rating": "8.0","transcode_width": "","labels": [],"stream_video_color_primaries": "bt709","stream_bitrate": "4934","bandwidth": "5181","optimized_version": 0,"video_language": "","parent_rating_key": "50","rating_key": "62","video_decision": "transcode","transcode_hw_decoding": 1,"thumb": "/library/metadata/62/thumb/1583237929","title": "Pilot","video_codec_level": "40","tagline": "","last_viewed_at": "1583269297","synced_version": 0,"transcode_audio_channels": "2","user_rating": "","platform": "Chrome","collections": [],"location": "lan","transcode_container": "mp4","audio_channel_layout": "5.1(side)","stream_duration": "4874976","local": 1,"stream_video_ref_frames": "","stream_video_width": "1920","transcode_hw_encode_title": "Intel (VA API)","stream_container_decision": "transcode","audience_rating": "","full_title": "Suits - Pilot","ip_address": "192.168.1.14","transcode_speed": "0.0","stream_subtitle_language": "Nederlands","channel_stream": 0,"video_bitrate": "3119","is_allow_sync": 1,"stream_video_bitrate": "4678","summary": "Harvey geeft Mike zijn eerste zaak, een pro-bono zaak waarbij een CEO wordt verdacht van seksuele intimidatie. Harvey moet zijn beste spel uit de kast trekken, om Mike gemotiveerd te houden.","stream_video_color_range": "","stream_audio_decision": "transcode","aspect_ratio": "1.78","audio_bitrate_mode": "","transcode_hw_decode_title": "Intel (VA API)","machine_id": "e8z5wabv80hjz27c5jylzghi","deleted_user": 0,"library_name": "Series","art": "/library/metadata/62/art/1583237929","stream_video_resolution": "1080","video_profile": "high","video_color_space": "bt709","sort_title": "","stream_video_codec_level": "","stream_video_height": "1080","year": "2011","duration": "4874976","video_scan_type": "progressive","progress_percent": "5","video_language_code": "","transcode_key": "/transcode/sessions/lofnrw2wr5qaz6emp72kbslb","transcode_throttled": 1,"container": "mkv","stream_audio_bitrate": "256","user": "Gerard Oedzes","selected": 1,"subtitle_location": "embedded","transcode_hw_requested": 1,"video_height": "1080","state": "paused","is_restricted": 0,"email": "[email protected]","stream_container": "mp4","stream_video_dynamic_range": "SDR","video_bit_depth": "8","stream_audio_sample_rate": "","grandparent_title": "Suits","studio": "USA Network","transcode_decision": "transcode","video_width": "1920","bitrate": "3503","stream_audio_channel_layout": "Stereo","originally_available_at": "2011-06-23","video_frame_rate": "23.976","synced_version_profile": "","friendly_name": "Gerard Oedzes","audio_profile": "","video_chroma_subsampling": "4:2:0","platform_name": "chrome","stream_video_language": "","keep_history": 1,"stream_subtitle_container": "","stream_audio_codec": "aac","stream_video_full_resolution": "1080p","video_codec": "h264","grandparent_thumb": "/library/metadata/49/thumb/1583237992","audio_bitrate": "384","transcode_hw_decode": "vaapi","secure": 1,"user_thumb": "https://plex.tv/users/9f5caff9a3cd56ab/avatar?c=1578427885","video_color_primaries": "bt709","height": "1080","stream_subtitle_decision": "burn","audio_codec": "ac3","parent_title": "Season 1","guid": "com.plexapp.agents.thetvdb://247808/1/1?lang=nl","stream_video_scan_type": "","audio_language_code": "eng","transcode_video_codec": "h264","transcode_audio_codec": "aac","stream_video_decision": "transcode","stream_video_color_trc": "bt709","user_id": 20020070,"transcode_height": "","transcode_hw_full_pipeline": 0,"throttled": "1","grandparent_guid": "com.plexapp.agents.thetvdb://247808?lang=nl","live": 0,"width": "1920","quality_profile": "Original","stream_subtitle_forced": 0,"media_type": "episode","video_resolution": "1080","stream_subtitle_location": "segments-video","do_notify": 1,"video_ref_frames": "4","stream_subtitle_language_code": "dut","product_version": "4.22.2","stream_audio_language_code": "eng","optimized_version_profile": "","rating_image": "","is_home_user": 0,"is_admin": 1,"ip_address_public": "213.93.65.245","allow_guest": 0,"video_color_range": "tv","audio_sample_rate": "48000","stream_audio_channel_layout_": "","media_index": "1","stream_video_framerate": "24p","transcode_hw_encode": "vaapi","grandparent_rating_key": "49","original_title": "","added_at": "1583236273","banner": "/library/metadata/49/banner/1583237992","bif_thumb": "","parent_media_index": "1","live_uuid": "","audio_language": "English","stream_audio_bitrate_mode": "cbr","username": "Gerard Oedzes","video_color_trc": "bt709","subtitle_decision": "burn","parent_guid": "com.plexapp.agents.thetvdb://247808/1?lang=nl","video_dynamic_range": "SDR","children_count": "","updated_at": "1583237929","video_full_resolution": "1080p","player": "Chrome","audio_channels": "6","subtitle_format": "","file": "/home/gerard/synology_share/ds218/multimedia/serie/Suits/season 1/suits S01E01 Pilot.mkv","file_size": "2134833392","session_key": "12","subtitles": 1,"id": "341","subtitle_container": "","optimized_version_title": "","genres": ["Comedy","Drama"],"stream_audio_channels": "2","stream_video_language_code": "","relayed": 0,"indexes": 0,"platform_version": "80.0","stream_audio_language": "English","writers": ["Aaron Korsh"],"actors": ["Aloma Wright","Gabriel Macht","Sarah Rafferty","Wendell Pierce","Patrick J. Adams","Abigail Spencer","Dulé Hill","Gina Torres","Amanda Schull","D.B. Woodside","Meghan Markle","Rachael Harris","Katherine Heigl","Rick Hoffman"],"stream_subtitle_format": "","audio_decision": "transcode","subtitle_forced": 0,"profile": "Web","product": "Plex Web","view_offset": "238000","type": "3","audience_rating_image": "","section_id": "3","stream_subtitle_codec": "","stream_video_color_space": "","subtitle_codec": "ass","stream_video_codec": "h264","device": "Windows","stream_video_bit_depth": "","video_framerate": "24p","transcode_hw_encoding": 1,"transcode_protocol": "dash","shared_libraries": ["1","2","3"],"stream_aspect_ratio": "","content_rating": "TV-14","session_id": "ppgvxw0s9rg4bq40ktm29gei","directors": ["Kevin Bray"],"parent_thumb": "/library/metadata/50/thumb/1583237889","subtitle_language_code": "dut","transcode_progress": 8,"subtitle_language": "Nederlands","stream_video_chroma_subsampling": ""}],"stream_count": "1","total_bandwidth": 5181,"stream_count_transcode": 1,"wan_bandwidth": 0,"stream_count_direct_play": 0,"lan_bandwidth": 5181,"stream_count_direct_stream": 0},"result": "success"}}'
        
        dz.utils.dumpTable(dz.utils.fromJSON(myJSONString))
    end    
}
Spoiler: show

Code: Select all

2020-03-04 21:11:53.550  Status: dzVents: Info: ------ Start internal script: JSON reader: Device: "JSONTrigger (Virtual)", Index: 122
 > response:
 >     data:
 >             stream_count_transcode: 1
 >             stream_count_direct_stream: 0
 >             sessions:
 >                     1:
 >                             video_color_trc: bt709
 >                             platform_name: chrome
 >                             progress_percent: 5
 >                             stream_audio_sample_rate:
 >                             stream_video_decision: transcode
 >                             subtitle_format:
 >                             is_allow_sync: 1
 >                             transcode_hw_encode: vaapi
 >                             stream_subtitle_decision: burn
 >                             audio_profile:
 >                             transcode_hw_decoding: 1
 >                             transcode_speed: 0.0
 >                             stream_audio_bitrate_mode: cbr
 >                             video_frame_rate: 23.976
 >                             stream_video_codec: h264
 >                             optimized_version: 0
 >                             shared_libraries:
 >                                     1: 1
 >                                     2: 2
 >                                     3: 3
 >                             state: paused
 >                             video_bitrate: 3119
 >                             audience_rating:
 >                             transcode_width:
 >                             tagline:
 >                             stream_video_language:
 >                             rating_key: 62
 >                             channel_stream: 0
 >                             session_key: 12
 >                             parent_thumb: /library/metadata/50/thumb/1583237889
 >                             stream_subtitle_language_code: dut
 >                             quality_profile: Original
 >                             secure: 1
 >                             video_language:
 >                             stream_audio_channels: 2
 >                             parent_title: Season 1
 >                             transcode_video_codec: h264
 >                             art: /library/metadata/62/art/1583237929
 >                             video_codec_level: 40
 >                             video_scan_type: progressive
 >                             email: [email protected]
 >                             user: Gerard Oedzes
 >                             location: lan
 >                             view_offset: 238000
 >                             bandwidth: 5181
 >                             audio_bitrate: 384
 >                             stream_audio_decision: transcode
 >                             grandparent_thumb: /library/metadata/49/thumb/1583237992
 >                             stream_subtitle_codec:
 >                             video_codec: h264
 >                             ip_address: 192.168.1.14
 >                             transcode_hw_full_pipeline: 0
 >                             optimized_version_title:
 >                             last_viewed_at: 1583269297
 >                             audio_channel_layout: 5.1(side)
 >                             file: /home/gerard/synology_share/ds218/multimedia/serie/Suits/season 1/suits S01E01 Pilot.mkv
 >                             is_home_user: 0
 >                             do_notify: 1
 >                             transcode_hw_encoding: 1
 >                             transcode_throttled: 1
 >                             genres:
 >                                     1: Comedy
 >                                     2: Drama
 >                             type: 3
 >                             video_chroma_subsampling: 4:2:0
 >                             stream_duration: 4874976
 >                             transcode_container: mp4
 >                             title: Pilot
 >                             aspect_ratio: 1.78
 >                             rating: 8.0
 >                             stream_video_bitrate: 4678
 >                             duration: 4874976
 >                             updated_at: 1583237929
 >                             subtitles: 1
 >                             subtitle_language: Nederlands
 >                             bif_thumb:
 >                             video_height: 1080
 >                             video_color_range: tv
 >                             subtitle_language_code: dut
 >                             directors:
 >                                     1: Kevin Bray
 >                             stream_video_resolution: 1080
 >                             stream_subtitle_format:
 >                             live: 0
 >                             product_version: 4.22.2
 >                             stream_aspect_ratio:
 >                             transcode_hw_decode_title: Intel (VA API)
 >                             stream_video_color_trc: bt709
 >                             video_language_code:
 >                             video_bit_depth: 8
 >                             device: Windows
 >                             video_framerate: 24p
 >                             stream_video_full_resolution: 1080p
 >                             stream_audio_language_code: eng
 >                             stream_video_bit_depth:
 >                             optimized_version_profile:
 >                             stream_video_color_space:
 >                             labels:
 >                             keep_history: 1
 >                             stream_audio_language: English
 >                             video_resolution: 1080
 >                             year: 2011
 >                             audio_sample_rate: 48000
 >                             transcode_progress: 8
 >                             selected: 1
 >                             audience_rating_image:
 >                             product: Plex Web
 >                             profile: Web
 >                             stream_video_color_primaries: bt709
 >                             video_color_space: bt709
 >                             user_rating:
 >                             audio_decision: transcode
 >                             content_rating: TV-14
 >                             video_decision: transcode
 >                             added_at: 1583236273
 >                             actors:
 >                                     1: Aloma Wright
 >                                     2: Gabriel Macht
 >                                     3: Sarah Rafferty
 >                                     4: Wendell Pierce
 >                                     5: Patrick J. Adams
 >                                     6: Abigail Spencer
 >                                     7: Dulé Hill
 >                                     8: Gina Torres
 >                                     9: Amanda Schull
 >                                     10: D.B. Woodside
 >                                     11: Meghan Markle
 >                                     12: Rachael Harris
 >                                     13: Katherine Heigl
 >                                     14: Rick Hoffman
 >                             deleted_user: 0
 >                             transcode_hw_decode: vaapi
 >                             writers:
 >                                     1: Aaron Korsh
 >                             platform_version: 80.0
 >                             relayed: 0
 >                             stream_audio_channel_layout_:
 >                             audio_codec: ac3
 >                             video_width: 1920
 >                             stream_video_language_code:
 >                             subtitle_container:
 >                             media_type: episode
 >                             file_size: 2134833392
 >                             stream_subtitle_location: segments-video
 >                             stream_audio_bitrate: 256
 >                             stream_video_ref_frames:
 >                             player: Chrome
 >                             video_full_resolution: 1080p
 >                             stream_video_chroma_subsampling:
 >                             indexes: 0
 >                             video_dynamic_range: SDR
 >                             transcode_audio_codec: aac
 >                             originally_available_at: 2011-06-23
 >                             live_uuid:
 >                             subtitle_decision: burn
 >                             username: Gerard Oedzes
 >                             transcode_hw_requested: 1
 >                             parent_guid: com.plexapp.agents.thetvdb://247808/1?lang=nl
 >                             stream_video_codec_level:
 >                             parent_media_index: 1
 >                             container: mkv
 >                             allow_guest: 0
 >                             section_id: 3
 >                             collections:
 >                             stream_subtitle_container:
 >                             original_title:
 >                             video_ref_frames: 4
 >                             grandparent_rating_key: 49
 >                             audio_bitrate_mode:
 >                             height: 1080
 >                             bitrate: 3503
 >                             media_index: 1
 >                             children_count:
 >                             stream_subtitle_language: Nederlands
 >                             stream_container: mp4
 >                             width: 1920
 >                             is_admin: 1
 >                             rating_image:
 >                             subtitle_codec: ass
 >                             platform: Chrome
 >                             session_id: ppgvxw0s9rg4bq40ktm29gei
 >                             library_name: Series
 >                             thumb: /library/metadata/62/thumb/1583237929
 >                             subtitle_forced: 0
 >                             is_restricted: 0
 >                             video_color_primaries: bt709
 >                             stream_video_width: 1920
 >                             id: 341
 >                             stream_subtitle_forced: 0
 >                             studio: USA Network
 >                             ip_address_public: 213.93.65.245
 >                             throttled: 1
 >                             transcode_key: /transcode/sessions/lofnrw2wr5qaz6emp72kbslb
 >                             transcode_protocol: dash
 >                             audio_language: English
 >                             synced_version: 0
 >                             transcode_height:
 >                             user_id: 20020070
 >                             grandparent_title: Suits
 >                             grandparent_guid: com.plexapp.agents.thetvdb://247808?lang=nl
 >                             stream_video_framerate: 24p
 >                             stream_video_height: 1080
 >                             stream_video_dynamic_range: SDR
 >                             video_profile: high
 >                             stream_audio_codec: aac
 >                             transcode_audio_channels: 2
 >                             full_title: Suits - Pilot
 >                             audio_language_code: eng
 >                             stream_video_scan_type:
 >                             sort_title:
 >                             guid: com.plexapp.agents.thetvdb://247808/1/1?lang=nl
 >                             user_thumb: https://plex.tv/users/9f5caff9a3cd56ab/avatar?c=1578427885
 >                             stream_video_color_range:
 >                             stream_audio_channel_layout: Stereo
 >                             parent_rating_key: 50
 >                             banner: /library/metadata/49/banner/1583237992
 >                             friendly_name: Gerard Oedzes
 >                             synced_version_profile:
 >                             local: 1
 >                             subtitle_location: embedded
 >                             transcode_hw_encode_title: Intel (VA API)
 >                             audio_channels: 6
 >                             stream_container_decision: transcode
 >                             stream_bitrate: 4934
 >                             summary: Harvey geeft Mike zijn eerste zaak, een pro-bono zaak waarbij een CEO wordt verdacht van seksuele intimidatie. Harvey moet zijn beste spel uit de kast trekken, om Mike gemotiveerd te houden.
 >                             machine_id: e8z5wabv80hjz27c5jylzghi
 >                             transcode_decision: transcode
 >             total_bandwidth: 5181
 >             stream_count_direct_play: 0
 >             stream_count: 1
 >             lan_bandwidth: 5181
 >             wan_bandwidth: 0
 >     result: success
 
2020-03-04 21:11:53.558  Status: dzVents: Info: ------ Finished JSON reader
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
OedzesG
Posts: 106
Joined: Monday 11 March 2019 0:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: how read json

Post by OedzesG »

It makes the reading of the JSON structure much easier

dumptable is working, but how can i select the right value?
( sorry for the noob quenstion... :oops: )

this gives an error:

Code: Select all

	
		local tautulli_ip 	= 'xxx'
		local tautulli_api 	= 'xxx'

		return {
		on = {
	    customEvents	= { 'plex',},
		httpResponses = { 'plex' }
		},
        execute = function(dz, item)

		if (item.isCustomEvent) then
        dz.openURL({
        url = 'http://' .. tautulli_ip .. '/api/v2?apikey=' .. tautulli_api .. '&cmd=get_activity', 
        callback = 'plex'
        })
		end
    
      
       plex_data = dz.utils.dumpTable(dz.utils.fromJSON(item.data))
       
       test = plex_data.rating
       
       dz.log(test)
            

 
		end 
	} 
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how read json

Post by waaren »

OedzesG wrote: Wednesday 04 March 2020 21:34 dumptable is working, but how can i select the right value?

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'JSONTrigger',  -- just an example to trigger the request
        },
    },
        
    execute = function(dz, item)

        local myJSONString = '{"response": {"message": null,"data": {"sessions": [{"rating": "8.0","transcode_width": "","labels": [],"stream_video_color_primaries": "bt709","stream_bitrate": "4934","bandwidth": "5181","optimized_version": 0,"video_language": "","parent_rating_key": "50","rating_key": "62","video_decision": "transcode","transcode_hw_decoding": 1,"thumb": "/library/metadata/62/thumb/1583237929","title": "Pilot","video_codec_level": "40","tagline": "","last_viewed_at": "1583269297","synced_version": 0,"transcode_audio_channels": "2","user_rating": "","platform": "Chrome","collections": [],"location": "lan","transcode_container": "mp4","audio_channel_layout": "5.1(side)","stream_duration": "4874976","local": 1,"stream_video_ref_frames": "","stream_video_width": "1920","transcode_hw_encode_title": "Intel (VA API)","stream_container_decision": "transcode","audience_rating": "","full_title": "Suits - Pilot","ip_address": "192.168.1.14","transcode_speed": "0.0","stream_subtitle_language": "Nederlands","channel_stream": 0,"video_bitrate": "3119","is_allow_sync": 1,"stream_video_bitrate": "4678","summary": "Harvey geeft Mike zijn eerste zaak, een pro-bono zaak waarbij een CEO wordt verdacht van seksuele intimidatie. Harvey moet zijn beste spel uit de kast trekken, om Mike gemotiveerd te houden.","stream_video_color_range": "","stream_audio_decision": "transcode","aspect_ratio": "1.78","audio_bitrate_mode": "","transcode_hw_decode_title": "Intel (VA API)","machine_id": "e8z5wabv80hjz27c5jylzghi","deleted_user": 0,"library_name": "Series","art": "/library/metadata/62/art/1583237929","stream_video_resolution": "1080","video_profile": "high","video_color_space": "bt709","sort_title": "","stream_video_codec_level": "","stream_video_height": "1080","year": "2011","duration": "4874976","video_scan_type": "progressive","progress_percent": "5","video_language_code": "","transcode_key": "/transcode/sessions/lofnrw2wr5qaz6emp72kbslb","transcode_throttled": 1,"container": "mkv","stream_audio_bitrate": "256","user": "Gerard Oedzes","selected": 1,"subtitle_location": "embedded","transcode_hw_requested": 1,"video_height": "1080","state": "paused","is_restricted": 0,"email": "[email protected]","stream_container": "mp4","stream_video_dynamic_range": "SDR","video_bit_depth": "8","stream_audio_sample_rate": "","grandparent_title": "Suits","studio": "USA Network","transcode_decision": "transcode","video_width": "1920","bitrate": "3503","stream_audio_channel_layout": "Stereo","originally_available_at": "2011-06-23","video_frame_rate": "23.976","synced_version_profile": "","friendly_name": "Gerard Oedzes","audio_profile": "","video_chroma_subsampling": "4:2:0","platform_name": "chrome","stream_video_language": "","keep_history": 1,"stream_subtitle_container": "","stream_audio_codec": "aac","stream_video_full_resolution": "1080p","video_codec": "h264","grandparent_thumb": "/library/metadata/49/thumb/1583237992","audio_bitrate": "384","transcode_hw_decode": "vaapi","secure": 1,"user_thumb": "https://plex.tv/users/9f5caff9a3cd56ab/avatar?c=1578427885","video_color_primaries": "bt709","height": "1080","stream_subtitle_decision": "burn","audio_codec": "ac3","parent_title": "Season 1","guid": "com.plexapp.agents.thetvdb://247808/1/1?lang=nl","stream_video_scan_type": "","audio_language_code": "eng","transcode_video_codec": "h264","transcode_audio_codec": "aac","stream_video_decision": "transcode","stream_video_color_trc": "bt709","user_id": 20020070,"transcode_height": "","transcode_hw_full_pipeline": 0,"throttled": "1","grandparent_guid": "com.plexapp.agents.thetvdb://247808?lang=nl","live": 0,"width": "1920","quality_profile": "Original","stream_subtitle_forced": 0,"media_type": "episode","video_resolution": "1080","stream_subtitle_location": "segments-video","do_notify": 1,"video_ref_frames": "4","stream_subtitle_language_code": "dut","product_version": "4.22.2","stream_audio_language_code": "eng","optimized_version_profile": "","rating_image": "","is_home_user": 0,"is_admin": 1,"ip_address_public": "213.93.65.245","allow_guest": 0,"video_color_range": "tv","audio_sample_rate": "48000","stream_audio_channel_layout_": "","media_index": "1","stream_video_framerate": "24p","transcode_hw_encode": "vaapi","grandparent_rating_key": "49","original_title": "","added_at": "1583236273","banner": "/library/metadata/49/banner/1583237992","bif_thumb": "","parent_media_index": "1","live_uuid": "","audio_language": "English","stream_audio_bitrate_mode": "cbr","username": "Gerard Oedzes","video_color_trc": "bt709","subtitle_decision": "burn","parent_guid": "com.plexapp.agents.thetvdb://247808/1?lang=nl","video_dynamic_range": "SDR","children_count": "","updated_at": "1583237929","video_full_resolution": "1080p","player": "Chrome","audio_channels": "6","subtitle_format": "","file": "/home/gerard/synology_share/ds218/multimedia/serie/Suits/season 1/suits S01E01 Pilot.mkv","file_size": "2134833392","session_key": "12","subtitles": 1,"id": "341","subtitle_container": "","optimized_version_title": "","genres": ["Comedy","Drama"],"stream_audio_channels": "2","stream_video_language_code": "","relayed": 0,"indexes": 0,"platform_version": "80.0","stream_audio_language": "English","writers": ["Aaron Korsh"],"actors": ["Aloma Wright","Gabriel Macht","Sarah Rafferty","Wendell Pierce","Patrick J. Adams","Abigail Spencer","Dulé Hill","Gina Torres","Amanda Schull","D.B. Woodside","Meghan Markle","Rachael Harris","Katherine Heigl","Rick Hoffman"],"stream_subtitle_format": "","audio_decision": "transcode","subtitle_forced": 0,"profile": "Web","product": "Plex Web","view_offset": "238000","type": "3","audience_rating_image": "","section_id": "3","stream_subtitle_codec": "","stream_video_color_space": "","subtitle_codec": "ass","stream_video_codec": "h264","device": "Windows","stream_video_bit_depth": "","video_framerate": "24p","transcode_hw_encoding": 1,"transcode_protocol": "dash","shared_libraries": ["1","2","3"],"stream_aspect_ratio": "","content_rating": "TV-14","session_id": "ppgvxw0s9rg4bq40ktm29gei","directors": ["Kevin Bray"],"parent_thumb": "/library/metadata/50/thumb/1583237889","subtitle_language_code": "dut","transcode_progress": 8,"subtitle_language": "Nederlands","stream_video_chroma_subsampling": ""}],"stream_count": "1","total_bandwidth": 5181,"stream_count_transcode": 1,"wan_bandwidth": 0,"stream_count_direct_play": 0,"lan_bandwidth": 5181,"stream_count_direct_stream": 0},"result": "success"}}'
        
        -- dz.utils.dumpTable(dz.utils.fromJSON(myJSONString))
        
        rt = dz.utils.fromJSON(myJSONString) -- store complete json in rt (returnTable)
        attributes = rt.response.data.sessions[1] -- all attributes from sessions[1]
        
        dz.log('Rating is : ' .. attributes.rating ,dz.LOG_FORCE)
    end    
}

Code: Select all

2020-03-04 21:50:02.951 Status: dzVents: Info: ------ Start internal script: JSON reader: Device: "JSONTrigger (Virtual)", Index: 122
2020-03-04 21:50:02.954 Status: dzVents: !Info: Rating is : 8.0
2020-03-04 21:50:02.954 Status: dzVents: Info: ------ Finished JSON reader
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
OedzesG
Posts: 106
Joined: Monday 11 March 2019 0:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: how read json

Post by OedzesG »

Thnx @waaren

i get the value right now... but also an error?

Code: Select all

2020-03-04 21:10:12.140 Status: dzVents: Info: ------ Start internal script: Script #2: HTTPResponse: "plex"
2020-03-04 21:10:12.146 Status: dzVents: !Info: Rating is : 6.7
2020-03-04 21:10:12.146 Status: dzVents: Info: ------ Finished Script #2
2020-03-04 21:10:12.045 Error: dzVents: Error: (3.0.1) An error occurred when calling event handler Script #2
2020-03-04 21:10:12.045 Error: dzVents: Error: (3.0.1) ...domoticz/scripts/dzVents/generated_scripts/Script #2.lua:23: attempt to index a nil value (global 'rt')

Code: Select all

		local tautulli_ip 	= 'xxx'
		local tautulli_api 	= 'xxx'

		return {
		on = {
	    customEvents	= { 'plex',},
		httpResponses = { 'plex' }
		},
        execute = function(dz, item)

		if (item.isCustomEvent) then
        
			dz.openURL	({
							url = 'http://' .. tautulli_ip .. '/api/v2?apikey=' .. tautulli_api .. '&cmd=get_activity', 
							callback = 'plex'
						})
		end
    
	local myJSONString = item.data
		
	
	rt = dz.utils.fromJSON(myJSONString) 
	attributes = rt.response.data.sessions[1] 
        
		dz.log('Rating is : ' .. attributes.rating ,dz.LOG_FORCE)
			
	end    
} 
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how read json  [Solved]

Post by waaren »

OedzesG wrote: Wednesday 04 March 2020 22:12 Thnx @waaren

i get the value right now... but also an error?
From these log lines I cannot see how the script was started. If the script was triggered by the customEvent the openURL will be send but the script will continue after that.
Please add a logging section and stop the script after the openURL

Code: Select all

local tautulli_ip = 'xxx'
local tautulli_api = 'xxx'

return 
{
    on = 
    {
        customEvents    = 
        { 
            'plex',
        },
        
        httpResponses = 
        { 
            'plex',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
        marker = 'Plex',
    },

    execute = function(dz, item)

        if item.isCustomEvent then
            dz.openURL(
                {
                    url = 'http://' .. tautulli_ip .. '/api/v2?apikey=' .. tautulli_api .. '&cmd=get_activity', 
                    callback = 'plex',
                })
                return
        end

        rt = dz.utils.fromJSON(item.data)
        attributes = rt.response.data.sessions[1] 

        dz.log('Rating is : ' .. attributes.rating ,dz.LOG_FORCE)

    end
} 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest