json Decode issue

84 views
Skip to first unread message
Assigned to ewpa...@gmail.com by carte...@gmail.com

TimAI2

unread,
Dec 21, 2018, 1:46:40 PM12/21/18
to MIT App Inventor Forum
When using the json decode text block on a simple json, on my system, it sorts the "key:pair"s alphabetically (and capitals go first)

blocksjsondecode.png


any workaround for this?


SteveJG

unread,
Dec 21, 2018, 3:50:40 PM12/21/18
to MIT App Inventor Forum
Use the Text  downcase block (find the upcase block and use the ^ to change its function to downcase) on the entire json to make Upper Case into Lower Case on the   get global json    block ahead of the decode block?

Does that work?

Regards,
Steve

TimAI2

unread,
Dec 21, 2018, 6:55:42 PM12/21/18
to MIT App Inventor Forum
That handles the capitals, so better, but doesn't resolve the automatic alphabetical sorting

{"c": "dog","D":"cow","a":"cat","E":"hen","b":"pig"}

should return:

((c dog) (D cow) (a cat) (E hen) (b pig))

TimAI2

unread,
Dec 22, 2018, 5:17:48 AM12/22/18
to MIT App Inventor Forum
Eventually came up with this, which is OK for a simple json string, but more work would be needed for nested etc.
Required significant string manipulation!

blockskeepjsonorder.png


SCREEN


screenjsonorder.png


must be missing something somewhere ??


 

SteveJG

unread,
Dec 22, 2018, 8:29:10 AM12/22/18
to MIT App Inventor Forum
I believe this behavior is probably a bug.  The behavior may possibly be related to  why the  Map FeatureCollectiion's is currently not handling a json string properly (t Evan is working on  correcting that right now). The decode block is not working as MIT expects.  If one constructs  downcase {a:Be,C:Be,E:Fe} the blocks produces ((a be) (c be) (e fe)) as  expected as described in the documentation.  Something is wrong that reorders based on case a 'sorting' order  of characters.

-- Steve




Chris Ward

unread,
Dec 22, 2018, 9:21:32 AM12/22/18
to MIT App Inventor Forum
Hi all

json decoders in other languages have the same trait - the data often does not decode in the order that it is encoded. This should not matter with an 'ID:Value' format - you could in fact sort the result on ID or Value after decode.

TimAI2

unread,
Dec 22, 2018, 10:13:10 AM12/22/18
to MIT App Inventor Forum
Yes, but sometimes the order of things as they come in is important.

FWIW, javascript keeps order

jsondecodejavascriptkeepsorder.png


ABG

unread,
Dec 22, 2018, 4:05:01 PM12/22/18
to MIT App Inventor Forum

TimAI2

unread,
Dec 23, 2018, 5:39:01 AM12/23/18
to mitappinv...@googlegroups.com
If you want to use javascript and the webviewstring to decode your simple json to an AI2 list, in order that the data is returned in the same order as the original json file, example as follows:

BLOCKS

blocksjsondecodejavascript.png


SCREEN


screenjsondecodejavascript.png

HTML



<!DOCTYPE html>
<html>
<body>
<script>
var y = '';
var myObj = JSON.parse(window.AppInventor.getWebViewString());
for (x in myObj) {   
y += x + ", " + myObj[x] + " : "; 
window.AppInventor.setWebViewString(y);  
</script>  
</body> 
</html>


I use the file component to create the html file on the sdcard on the fly

For some reason the webviewer would not accept an "\n" in the decoded string so substituted with a ":" and did some text replacement on return (this works OK in Chrome on my PC)

As always, file paths may differ....



jsonDecodeJavaScript.aia
Reply all
Reply to author
Forward
0 new messages