Json string to Dictionary

Hi all,

Unless I'm mistaken, there is no builtin class or function to do it. For the need of my widget (APICall), I coded a function to do it.
I'm not a professionnal developper, so it surely not perfect but do the job.

I followed this description of the grammar : www.json.org/json-fr.html.
But do some minor changes :
- The "name" part of an objet can be defined without "double quote" if start with a letter and have only letter, digit, -, _ or . Ex : {name:"value"} is ok.
- It only accept space as "whitespace", no tab, carrier return etc ... but you can change the function <read_spaces> to change this behavior.
- It accept there is not "whitespace" beetween any element of the grammar.
- It doesn't correctly interprete some escaped characters in a string (\\, \/ and \" are OK, but \b \f \n \r \t and \u are not interpreted).

It's only a set of functions not a class. You just have to call json_to_dict(my_json_str_convert). It will return a dictionnary.
If there is a syntax error, it only return null, it is perhaps the main improvement to do ;o)

Enjoy !

Source code here : https://apicall.dumesnil.net/APICallJson.html

  • Unless you can optimize your code, the way to avoid the watchdog, is to do things in parts.  Do part of the processing, set a timer and return to the VM, and when the timer fires, do part 2, and so on.

  • Many thanks Travis. I finally succeeded to add the function in intial post. There is still a problem on line 9.

    tmp_char.equals(+) is accepted but not tmp_char.equals("+")... anyway all code is at the same place now.

  • Hi, nice solution but you forgot to put function read_tuple().

    It is a shame that Garmin not expose json -> dic parser, although they must use it internally to parse http messages to dictionary

  • The parser is in Connect on the phone, some devices that have Wi-Fi must have a internal parser if makeWebRequest can go over the latter connection.

    Though making the user type it in, in json, added additional work for the user, as well as developer.  There were and are simpler solutions to the problem.

  • Nope.  With a makeWebRequest and json data, you always see a dictionary if the request uses BT to the phone or wifi.  The conversion is done on the Garmin Device.  You'll see this on edge devices where the wifi connection is on if no activity is being recorded.  On a watch, you'll rarely have a wifi connection.

  • Presumably you know this because you have scanned the Bluetooth traffic on non Wi-Fi models?

  • No, been doing widgets for 8 years.  Nothing needs to be done for json data over wifi.,An app sees it as a dictionary if it came from BT or wifi.

    If you do "View HTTP Traffic" in the sim, what you always see is JSON data

  • I send JSON text via BT and on phone message callback I get only String Object, not Dictionary Object. I don't see any parser or converter to change it to Dic. Missing constructor like Dictionary(JSONString) or sth like toDictionary().

  • Are you using

    :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
    and not
    :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN
    What do you see in the sim with "View HTTP Traffic"?
  • If you do "View HTTP Traffic" in the sim, what you always see is JSON data

    Well of course, because you'd also see that if the phone was doing the conversion.  The http traffic doesn't tell you where conversion is happening.