Decode JSON

Moderator: leecollings

Post Reply
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Decode JSON

Post by Egregius »

Can you use PHP? Then it would be easy :P

Code: Select all

<?php
$data=json_decode(file_get_contents('http://ip:port/json.htm?type=devices&rid=10'),true);
$co2 = filter_var($data['result']['0']['Data'], FILTER_SANITIZE_NUMBER_INT);
echo $co2;
$co2 contains only the numbers, so would echo 693 ;)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Decode JSON

Post by simonrg »

- Send the request (openurl?) ? - use the Lua socket library
- parse the json reply in LUA ? - use the Lua json library
- Get only the data part (693) ? - select from the table created when the json is decoded

If you have a look at the code for the smappee Lua script then you will see an example of how to do this - http://www.domoticz.com/wiki/Smappee
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Decode JSON

Post by simonrg »

Lisa wrote: Since Domoticz is running on windows, linux examples won't help me. 'use the Lua socket library' this isn't helpfull (besides the fact that the socket library isn't implemented in Domoticz)... :(
To jumpstart the Lua coding part, i'm in need of coding examples so that i can copy-paste the examples pieces i need. An API can be interpreted many ways, the examples create a shortcut to use the API...
I'm not an experienced LUA developer, nor do i wish to become one. The only reason i'm doing this, is to create a work-around for a bug in Domoticz...
Sorry to give you misleading advice, it would be helpful if you added a description of what you are running in your signature, then people wouldn't give you duff advice.

Any Lua library is available in Domoticz, if the platform on which you are running it has it implemented, so Windows or Linux are often both possible, I think the socket library is available for Windows, I don't think the SSL library is though.

But if you don't want to hack enough Lua to get this to work, then you could always execute a Curl / jq commands once again the Wiki has a number of examples of the use of Curl and jq from Lua.

If you are only doing this to get around a bug in Domoticz, then it would seem more effective to explain the bug clearly enough so the developer's can more easily fix the Domoticz code.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
hannibal2206
Posts: 10
Joined: Monday 08 August 2016 23:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: Decode JSON

Post by hannibal2206 »

Do you have a pointer where to find the solution ?
tequila
Posts: 60
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Re: RE: Re: Decode JSON

Post by tequila »

hannibal2206 wrote:Do you have a pointer where to find the solution ?
have a look at this:
https://www.domoticz.com/wiki/Battery_level_check

it's a good example of how to read a value using jquery, including how to get jquery itself.
hannibal2206
Posts: 10
Joined: Monday 08 August 2016 23:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: Decode JSON

Post by hannibal2206 »

jquery ? sorry, did not find that there
tequila
Posts: 60
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Re: RE: Re: Decode JSON

Post by tequila »

hannibal2206 wrote:jquery ? sorry, did not find that there
right at the top of the page is a description how to install jq. below is a nice script reading values using jq.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest