B4J Question Parsing Json data problem

hakha4

Member
Licensed User
Longtime User
I'm working on a B4J app retrieving Json data from a home control program (Domoticz) for reading status of devices etc. The problem is that Domoticz seems not to save strings in quotes (I think this is the problem?), giving error like :

ERROR: org.json.JSONException: Unterminated object at character 45 of [{HardwareName=ONKYO, LastUpdate=2020-06-28 10:37:23, PlanID=0, PlanIDs=[0], Image=Amplifier, Unit=0, DimmerType=none, MaxDimLevel=100, Name=Onkyo - Master selector, HardwareTypeVal=104, ID=00000008, Status=Set Level: 30 %, Protected=false, SubType=Selector Switch, LevelInt=30, AddjMulti=1.0, LevelActions=T2ZmfDEwfDAxfDAyfDExfDA1fDAzfDI1fDI0fDIzfDEyfDIyfDJifDJl, SwitchType=Selector, AddjMulti2=1.0, Level=30, idx=24, Notifications=false, AddjValue2=0.0, Timers=false, Favorite=1, HaveDimmer=true, Description=, HardwareID=4, XOffset=0, IsSubDevice=false, ShowNotifications=true, HardwareType=Onkyo AV Receiver (LAN), HaveTimeout=false, TypeImg=Light, SelectorStyle=1, BatteryLevel=255, HaveGroupCmd=true, SwitchTypeVal=18, SignalLevel=-, Data=Set Level: 30 %, YOffset=0, LevelOffHidden=true, Type=Light/Switch, UsedByCamera=false, LevelNames=T2ZmfEJEL0RWRHxDQkwvU0FUfEdBTUV8U1RSTSBCT1h8UEN8QVVYfEFNfEZNfENEfFRWfFBIT05PfE5FVHxCT...

I can't figure out how to get round this. Is there another approach to retrieve Json data then jsonparser that would work?
Any help/ideas appreciated
Regards Håkan
 

drgottjr

Expert
Licensed User
Longtime User
looks more like a csv string to me. read it in as a text string, then dim an array and split the string on ",", then step through the array. you could do other stuff with each element in the array, if desired.

with csv strings, there is always the problem of items that contain a comma and/or are quoted. normally, you would use a csv-parser to handle those problem, but Domoticz seems to avoid using items with commas or quotes, so a simpled regex split on comma would break down the string into a discrete array of attributes or properties. i think the effect would be the same as if Domoticz had used json. do they say in the documentation that they use json?
 

Attachments

  • capture.png
    capture.png
    39.6 KB · Views: 148
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The problem is that Domoticz seems not to save strings in quotes
1) I know nothing of Domoticz
2) Sample output of Domoticz found on the web show quoted strings
3) Looks like there is a plugin available for Domoticz that interfaces with Onkyo equipment. I've not found ant JSON outputs for this plugin. Therefore I'm guessing that this may be an issue with the plugin

4) Techically, the output above is not JSON, since neither keys nor values are quoted. Hard for JSONParser to support such "corrupted" input
 
Upvote 0

hakha4

Member
Licensed User
Longtime User
Thanks both for reply. Actually when doing parsing with Jsonparser I get correct parsing in most of the requested data, But according to Murphys laws it hangs on the parts that I'm most interrested of (information about device status) ! General system information returns ok, probably due to not having any spaces,or missing values after '='.

Yes, on wiki for Domoticz (https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Retrieve_status_of_Domoticz_instance) they claim that it is Json and as I said I can retrieve some meaningful data but not all. And data is enclosed in brackets ("[{", giving you the idea that it might be Json data.
I'm familiar with the plugin for Onkyo since I have one Onkyo reciever and communication between reciever and Domiticz work ok. If I request one of Onkyos properties it gives error and strings are not quoted!

(ERROR: org.json.JSONException: Unterminated object at character 45 of [{HardwareName=ONKYO, LastUpdate=2020-03-26 09:42:40, PlanID=0, PlanIDs=[0], Image=Amplifier, Unit=0, DimmerType=none, MaxDimLevel=100, Name=Onkyo - Zone 2 selector, HardwareTypeVal=104, ID=00000009, Status=Set Level: 130 %, Protected=false, SubType=Selector Switch, LevelInt=130, AddjMulti=1.0, LevelActions=T2ZmfDEwfDAxfDAyfDExfDAzfDI1fDI0fDIzfDEyfDIyfDJifDJlfDgw, SwitchType=Selector, AddjMulti2=1.0, Level=130, idx=25, Notifications=false, AddjValue2=0.0, Timers=false, Favorite=0, HaveDimmer=true, Description=, HardwareID=4, XOffset=0, IsSubDevice=false, ShowNotifications=true, HardwareType=Onkyo AV Receiver (LAN), HaveTimeout=false, TypeImg=Light, SelectorStyle=1, BatteryLevel=255, HaveGroupCmd=true, SwitchTypeVal=18, SignalLevel=-, Data=Set Level: 130 %, YOffset=0, LevelOffHidden=true, Type=Light/Switch, UsedByCamera=false, LevelNames=T2ZmfEJEL0RWRHxDQkwvU0FUfEdBTUV8U1RSTSBCT1h8QVVYfEFNfEZNfENEfFRWfFBIT05PfE5FVHxCT...)

When requesting data from Domoticz this is done direct to it's API and not to plugins I believe.

I give up Jsonparsing and try to see if I can code for a 'CSV splitter' and split data as 'CSV'.

Regards Håkan
 
Upvote 0

hakha4

Member
Licensed User
Longtime User
drgottjr says : looks more like a csv string to me. read it in as a text string, then dim an array and split the string on ",", then step through the array. you could do other stuff with each element in the array, if desired.
With your code snippet I got things to work now, thank's
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
what Domoticz says their device sends and what you showed are clearly not the same thing; their dox shows json. what you showed was csv. i saw the "[{" at the star, but those 2 chars were the only json in the entire string you showed. not sure how you're managing to turn json into csv, but if you'd care to post the full string, i wouldn't mind seeing exactly what the device outputs.
 
Upvote 0

emexes

Expert
Licensed User
split the string on ","
Even better, split on ", " ie comma space, not just comma.

Or add an extra ", " separator at the end, and then do a regex matcher eg:
1594032477803.png

edit: just realized we're in B4R and thus reaching for regex matcher might be a tad optimistic :oops:
 
Last edited:
Upvote 0
Top