Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

recursive json parsing

1,468 views
Skip to first unread message

Robert Spiteri

unread,
Jul 21, 2015, 4:01:07 AM7/21/15
to mitappinv...@googlegroups.com
Hi,

I have a json file with sub categories as shown below.

{
    "items": {
        "shelf": [
            {
                "item": [
                    {
                        "details": {
                            "material": "Cotton",
                            "name": "Shirt 1",
                            "prices": {
                                "price": [
                                    {
                                        "_name": "original",
                                        "__text": "50"
                                    },
                                    {
                                        "_name": "discount1",
                                        "__text": "30"
                                    },
                                    {
                                        "_name": "discount2",
                                        "__text": "20"
                                    }
                                ]
                            }
                        }
                    },
                    {
                        "details": {
                            "material": "Cotton",
                            "name": "Shirt 2",
                            "prices": {
                                "price": [
                                    {
                                        "_name": "original",
                                        "__text": "50"
                                    },
                                    {
                                        "_name": "discount1",
                                        "__text": "30"
                                    },
                                    {
                                        "_name": "discount2",
                                        "__text": "20"
                                    }
                                ]
                            }
                        }
                    }
                ],
                "_number": "1"
            },
            {
                "item": [
                    {
                        "details": {
                            "material": "Cotton",
                            "name": "Shirt 3",
                            "prices": {
                                "price": [
                                    {
                                        "_name": "original",
                                        "__text": "50"
                                    },
                                    {
                                        "_name": "discount1",
                                        "__text": "30"
                                    },
                                    {
                                        "_name": "discount2",
                                        "__text": "20"
                                    }
                                ]
                            }
                        }
                    },
                    {
                        "details": {
                            "material": "Cotton",
                            "name": "Shirt 4",
                            "prices": {
                                "price": [
                                    {
                                        "_name": "original",
                                        "__text": "50"
                                    },
                                    {
                                        "_name": "discount1",
                                        "__text": "30"
                                    },
                                    {
                                        "_name": "discount2",
                                        "__text": "20"
                                    }
                                ]
                            }
                        }
                    }
                ],
                "_number": "2"
            }
        ]
    }
}

How can I recursively parse the json to, for example, get the discount1 price of Shelf 2, item 2?

I need to be able to search by passing the values "discount1", 2 and 2 respectively.

Is it possible please? Since I have been searching online but the tutorials I found specify the item index directly.

Thanks

Abraham Getzler

unread,
Jul 21, 2015, 9:05:19 AM7/21/15
to mitappinv...@googlegroups.com
The Web1 JSON Text Decode block will turn your JSON string into a 17 level deep tree (list of lists of lists ...)
that can be climbed using techniques shown here for a XML decode (same technique).

For your specific navigation problem, I recommend building a list of branching instructions from the root for each desired destination,
using my wife's navigation style:  
  Take branch 2,
  then take branch 2,
  ...
  then take branch "discount1"   (notice the use of text implies a pair lookup here versus an index lookup)

To help you prepare your routes, I'm attaching a general purpose JSON browser routine, pre-loaded with your sample.

ABG



browse_JSON.png
Screenshot 2015-04-01 23.06.32.png
Screenshot 2015-04-01 23.07.44.png
showDepth.jpg
Web1_GotText.png
Back Button and Notifier.jpg
back.jpg
btnLoad_Click.png
Designer.jpg
globals.jpg
Initialization.jpg
JSON_Feeds.aia
ListView1_AfterPicking.jpg
Screenshot 2015-04-01 23.05.10.png
Screenshot 2015-04-01 23.05.35.png

Taifun

unread,
Jul 21, 2015, 9:51:03 AM7/21/15
to mitappinv...@googlegroups.com
great example!
Taifun

Abraham Getzler

unread,
Jul 21, 2015, 11:57:07 AM7/21/15
to mitappinv...@googlegroups.com
On closer examination of my example,  I cleaned it up a little,
removing a redundant assignment and renaming my stack and two buttons.

ABG

2015-07-21 11_41_15-5554__build_.png
Initialization.jpg
JSON_Feeds.aia
ListView1_AfterPicking.jpg
showDepth.jpg
Web1_GotText.png
2015-07-21 11_42_27-5554__build_.png
2015-07-21 11_43_52-5554__build_.png
Back Button and Notifier.jpg
back.jpg
browse_JSON.png
btnLoad_Click.png
Designer.jpg
globals.jpg

James H

unread,
Jul 22, 2015, 7:56:23 PM7/22/15
to MIT App Inventor Forum
What a great answer !

Robert Spiteri

unread,
Jul 23, 2015, 3:01:46 AM7/23/15
to MIT App Inventor Forum, abb...@gmail.com
WOW!!! I am really impressed with this answer...

thanks a million for your time Abraham Getzler and well done for this... that is exactly what I was looking for...

Abraham Getzler

unread,
Jul 23, 2015, 8:29:09 AM7/23/15
to MIT App Inventor Forum, rspit...@gmail.com
Thank you.

There's room in this sample for further enhancement,
if any one is feeling ambitious.

With the addition of a second stack for the selection indices,
it should be possible to gather together the list of choices that led
to the current branch of the tree.

Further thought would be needed to determine which of the
choices were based on choice by tag versus choice by browse.

ABG

Reply all
Reply to author
Forward
0 new messages