Parsing JSON with JQ

Moderator: leecollings

Post Reply
delchrys
Posts: 51
Joined: Wednesday 02 March 2016 20:52
Target OS: -
Domoticz version:
Contact:

Parsing JSON with JQ

Post by delchrys »

Hello all, I'm busy with a script and I need to parse some json data.
I do also is on request on my smartmeter and it gives a nice output. From the data field I would like to split the value after the ";" can someone guide me in how to do that with jQ parser?
I now have .result[] ."Data"
It gives me "433777;877448;0;0;488"
But I only want the first value, so I need to cut it or split it after every ";".
But I can't seem to get it right in jqplay.org

Anyone who could help me with it?
User avatar
galadril
Posts: 824
Joined: Monday 07 September 2015 10:32
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Contact:

Re: Parsing JSON with JQ

Post by galadril »

is it a python script?

Code: Select all

tim = '16:30:10'
hrs, mins, secs = tim.split(':')
or bash?

Code: Select all

#!/usr/bin/env bash

IN="[email protected];[email protected]"

mails=$(echo $IN | tr ";" "\n")

for addr in $mails
do
    echo "> [$addr]"
done
Solar panels of Ginlong, Omnik-Solar, Transenergy or Solarman?? Try my Android app:
https://play.google.com/store/apps/deta ... ongmonitor
oopee
Posts: 40
Joined: Monday 12 August 2013 11:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.x
Location: Finland
Contact:

Re: Parsing JSON with JQ

Post by oopee »

I use curl and jq in a bash script like this:

Code: Select all

temperature1=$(/usr/bin/curl -S -s 'http://dom.ipa.ddr.ess:8080/json.htm?type=devices&rid=172' | /home/pi/jq-1.5/jq -r .result[]."Temp")
delchrys
Posts: 51
Joined: Wednesday 02 March 2016 20:52
Target OS: -
Domoticz version:
Contact:

Re: Parsing JSON with JQ

Post by delchrys »

I use it in a bash script, but now I've got jq result[].Data | cut - d ";" -f1
It gives me the first value of "12345;67892;112233;2234;0"
So it gives "12345
I'm a bit further now I only need to lose the first ".
But how?
delchrys
Posts: 51
Joined: Wednesday 02 March 2016 20:52
Target OS: -
Domoticz version:
Contact:

Re: Parsing JSON with JQ

Post by delchrys »

Ahh I got it, it's :
jq -r .result[]Data | cut - d ";" -f1
So it's the -r that does the trick, don't know why yet. Have to figure out from the manual.
Thanks for the efforts.
VladGets
Posts: 3
Joined: Thursday 10 November 2016 14:27
Target OS: Windows
Domoticz version:
Contact:

Re: Parsing JSON with JQ

Post by VladGets »

galadril wrote:is it a python script?

Code: Select all

tim = '16:30:10'
hrs, mins, secs = tim.split(':')
or bash?

Code: Select all

#!/usr/bin/env bash

IN="[email protected];[email protected]"

mails=$(echo $IN | tr ";" "\n")

for addr in $mails
do
    echo "> [$addr]"
done
if you are interested in this questions, you can read more this blog http://djangostars.com/blog/python-in-a-1000-words/
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest