Page 1 of 1

curl (bash) and JSON

Posted: Tuesday 05 May 2020 15:54
by Number8
Hello,
i'm trying to use curl on bash, as a mean to send to Domoticz via customerevents a JSON data string. I having problems with [] in bash.
The following code is sent to Domoticz, fires up the related custom event and is recognized as a valid JSON data structure

Code: Select all

data='\{"sensor":\{"port":2,"output":1,"power":1.13619548\},"status":"success"\}'
url=$DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=customevent&event=mFI&data='$data
curl --silent $url > /dev/null
With data variable including a table in the JSON string, it does not even fireup custom event

Code: Select all

data='\{"sensors":[\{"port":2,"output":1,"power":1.13619548\}],"status":"success"\}'
I guess it is a matter of escaping [], I searched a lot with no success so far
dzvents 3.04

Re: curl (bash) and JSON  [Solved]

Posted: Tuesday 05 May 2020 18:07
by waaren
Number8 wrote: Tuesday 05 May 2020 15:54 i'm trying to use curl on bash, as a mean to send to Domoticz via customerevents a JSON data string. I having problems with [] in bash.
I guess it is a matter of escaping [], I searched a lot with no success so far
If you use the -g or --globoff option of curl you can leave out the escapes.

try:
data='{"sensors":[{"port":2,"output":1,"power":1.13619548}],"status":"success"}'
url="$DOMOTICZSERVER:$DOMOTICZPORT/json.htm?type=command&param=customevent&event=mFI&data=$data"
curl --globoff --silent $url > /dev/null

Re: curl (bash) and JSON

Posted: Tuesday 05 May 2020 18:52
by Number8
sooooooooooooo easy. Thanks @waaren I sweat on this one