[JSONPATH] Remove additional bracket / quotation sign

Can anyone help me with the JSONPATH extraction of my (3D) Print job?
By the way: an item starting with a number (3) is not valid, but thats fine.

I can’t remove the last brackets:

item:

String 	three_d_printer_job 	"3D Druckauftrag" 	(three_d_printer)		{http="<[3dprinter:15000:JSONPATH($.data..job)]"}

API Output (Repetier Firmware on a 3D Printer)

{"data":[{"active":true,"analysed":1,"done":6.8686644224222713,"job":"Desk_Organizer_new","jobid":55,"linesSend":29055,"name":"P802M","ofLayer":377,"online":1,"paused":false,"printTime":37158.890468541649,"printedTimeComp":8028.7504443682865,"slug":"P802M","start":1473748027,"totalLines":423008}]}

The result will be [“Desk_Organizer_new”]

Can i remove the bracket and, in case of Strings, also the quotation marks?

Thank you in advance

I don’t know how to do it with JSONPATH, but you could do it with a REGIX, something like REGIX(.“job”:"[.]".*)

I’m on my phone so can’t test it.

You could also use a JavaScript transform and select and manipulate the strings in JavaScript code.

1 Like

@sptzdmnc,
Here’s a runnable rule that’ll extract it. You can use it to try other combo’s as well:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

rule "JSON Test"
  when 
    Time cron "0/10 * * * * ?"
  then
    var String json = '{"data":[{"active":true,"analysed":1,"done":6.8686644224222713,"job":"Desk_Organizer_new","jobid":55,"linesSend":29055,"name":"P802M","ofLayer":377,"online":1,"paused":false,"printTime":37158.890468541649,"printedTimeComp":8028.750444368286
5,"slug":"P802M","start":1473748027,"totalLines":423008}]}'

    var String type = transform("JSONPATH", "$.data[0].job", json)
    logInfo("json", type)
end
1 Like

Thanks @rlkoshak and @guessed

I might follow the advice from Mark, because it could be, that i have to do some calculations within the rule.
Regex will work, of course.

The “Json Test” works great, if i handle the items as string items.
What if i want to extract a Float or Integer value?
(For example the percentage called “done”)

I really appreciate your support :slight_smile:

If all you want is the number then just assign it to a Number Item. If you want to do math with it in your rule use Float::parseFloat(are) which returns a float primitive.

1 Like

okay, and with integer type?

var int 	job_duration 		= Float::parseFloat(transform("JSONPATH", "$.data[0].printTime", three_d_printer_JSON.state.toString)).intValue()

This is my approach, beacause the value could be a float value.
But, do i need the Float::parseFloat ("")

in case i only want to have the integer value?

If the String being parsed can ever have a decimal in it you must use Float. Integer::parseInt will throw an exception if it encounters a decimal point.

1 Like