Page 1 of 1

Parsing JSON with JQ

Posted: Sunday 13 November 2016 16:04
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?

Re: Parsing JSON with JQ

Posted: Sunday 13 November 2016 18:11
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

Re: Parsing JSON with JQ

Posted: Sunday 13 November 2016 19:15
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")

Re: Parsing JSON with JQ

Posted: Sunday 13 November 2016 21:08
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?

Re: Parsing JSON with JQ

Posted: Sunday 13 November 2016 21:23
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.

Re: Parsing JSON with JQ

Posted: Monday 14 November 2016 14:23
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/