Simple Json Parser

Getting key:value pairs out of a json is not the most straight forward thing, especially when nested or those that have arrays. Json data structures appear to be infinitely variable in layout and syntax, and this can make it difficult to create a universal parser. My first effort (VERSION 1) works well with key:value pairs that are well formed (e.g. both are in double quotes. VERSION 2 takes things a little further. Still no guarantees either will cope with every json string....


In this revised effort on a json parser, it was back to the drawing board. I found I needed to use a regex extension to get where I wanted to go, therefore it canot run in a self contained block and an aia project is required for testing. I use a couple of listpickers to present the sample jsons for selection, and the keys.  The parser seems to cope well with json structures where there are no double quotes on the value side, for when a number is used or a boolean value (true/false/null). It also appears to handle long strings with commas and spaces in them (as long as they are surrounded by double quotes). Nested structures and arrays are also accommodated, but nesting only goes down one level, however this may be enough to return something. You do lose the first part of the nesting/array, however. For example if the json is "wind": { "speed":45,"direction":"NW"} it will return the key:value pairs of speed and direction, but nothing for wind.  The work of the parser should be enough to get something back from the json, which one can the do further work with to get the required value.

BLOCKS (well the vital one)



This looks horribly complicated but once broken down it become more understandable. There are four sections, each separated by an | (or).

In the procedure above, I load the json string to the extension which applies the regex matching.

1.  ("([^"]+)"\s*:\s*"([^"]+)"([^"]+)"\s*:\s*"([^"]+)")

This finds any nested/array structures and returns them

2. ("([^"]+)"\s*:\s*"([^"]+)")

This finds a standard double quoted key:value pair

3. ("([^"]+)"\s*:\s*(\d+\.?\d*))

This finds a value which is a number

4. ("([^"]+)"\s*:\s*(\w+))

This finds a value that is a word (e.g true/false) that has no double quotes


We eventually end up with a list of lists that contains each key:value pair from the json string



kevinkun on the Thunkable Classic forum for the regex extension

If you want to work with regex, you can use this useful web platform: RegExr to test out your matching code

Saj Duttal for the list sorting blocks 


Got fed up with the difficulties of getting a value out of a json string using key:value pairs, and none of the other options seemed to work by converting to lists, so decided to write my own "simple" parser, which strips all the json coding apart from the colons, and returns key:value pairs where there are key:value pairs (so strips out sub keys). This seems to work for straight forward json strings and more complex ones with arrays (should return all array values with same key), but it is quite possible a complex json might break this! See the video which demonstrates how it works for different json strings. I have zipped up the jsonParse procedure block, which should mean, once unzipped it can be drag and dropped onto an aia project.

This is not meant to be the definitive and complete json parsing method, but it seems to work in most situations. further testing has shown this will break if the key or value includes spaces or comments; so "temp":"45" will work,  "inside temp":"Slightly warmer, but comfortable" will break it.  Looked at fixing this but the complexities and variety of json structures are such it is not straight forward! May get somewhere with some regex...


Zip File of Procedure