Forum for discussing general topics related to Couch.
7 posts Page 1 of 1
Hello,

I need to create JSON format as follows:

Code: Select all
{
    "countries" :
    [
        {
            "name" : "Australia",
            "tagline" : "This is the tagline for Australia",
            "cities" :
            [
                {
                    "name" : "Perth",
                    "suburbs" :
                    [
                        {
                            "name" : "Innaloo"
                        },
                        {
                            "name" : "Booragon"
                        }
                    ]
                }
            ]
        }
    ]
}


I was able to get a single level JSON, but trying to get a multilevel one is doing my head in. :)

Thanks
Said
Hi Said,

Can you please post in how the 'countries', 'cities' and 'suburbs' are defined?
Also how are they related (presumably you are using the 'relations' feature).

Thanks.
Hi KK,

Sorry about that, I'm using folders for all of them and the way I've done it is Country is Top folder, City sub-folder of Country and suburb sub-folder of city, see attached image

Thanks & Regards
Said

Attachments

Hi KK,

For additional info, this is what I have attempted so far:

Code: Select all
<cms:content_type 'application/json'/>
{
   "countries":
   [
      {
<cms:folders masterpage='malls.php' orderby='k_folder_title' order='asc' hierarchical='1' extended_info='1' depth='5'>
   <cms:if k_level_start >
      "country":"<cms:show k_folder_title />",
      "tagLine":"<cms:show k_folder_desc />",
      "countryImage":"<cms:show k_folder_image />",
      "Count":"<cms:if><cms:show k_folder_totalchildren /><cms:else/><cms:show k_folder_totalpagecount/></cms:if>"</cms:if>
      <cms:if k_element_start >"city":"<cms:show k_folder_title />"</cms:if>
   }</cms:folders>
      }
   ]
}


Thanks & Regards
Said
Hi Said,

Following is the code I came up with to output the JSON format you specified
Code: Select all
<cms:content_type 'application/json'/>
{
    "countries" :
    [
   
    <!-- list countries -->
    <cms:set my_country_count='0' />
    <cms:folders masterpage='malls.php' order='desc' depth='1'>
        {
            "name" : "<cms:show k_folder_title />",
            "tagLine" : "<cms:show k_folder_desc />",
            "cities" :
            [
            <!-- list cities of the country -->
            <cms:set my_city_count='0' />
            <cms:if k_folder_immediate_children >
                <cms:folders masterpage='malls.php' order='desc' childof=k_folder_name depth='1'>
                    {
                        "name" : "<cms:show k_folder_title />",
                        "suburbs" :
                        [
                        <!-- list suburbs of the city -->
                        <cms:set my_suburb_count='0' />
                        <cms:if k_folder_immediate_children >
                            <cms:folders masterpage='malls.php' order='desc' childof=k_folder_name depth='1'>
                                {
                                    "name" : "<cms:show k_folder_title />"
                                }
                               
                                <cms:incr my_suburb_count />
                                <cms:if my_suburb_count!=k_total_folders> , </cms:if>
                            </cms:folders>
                        </cms:if>
                        ]
                    }
                   
                    <cms:incr my_city_count />
                    <cms:if my_city_count!=k_total_folders> , </cms:if>
                </cms:folders>
            </cms:if>
            ]
        }
       
        <cms:incr my_country_count />
        <cms:if my_country_count!=k_total_folders> , </cms:if>
    </cms:folders>
    ]
}

A short description of the code -
Since we have a fixed hierarchy with a maximum depth of 3 levels, I chose to use the cms:folders thrice (one for each level).
Each item in the JSON array needs to be terminated by a comma (except the lat item). This was handled by using a counter for each level (my_city_count etc.).

Please remove all <!-- --> comments from the code as those are only to make things clear for you.

With the code above, the following folder hierarchy -
Singapore
Phillipines
- Pasay City
- Bay City
Australia
- Sidney
-- Paramatta
- Perth
-- Innaloo
-- Canington
-- Booragon

- results in the following JSON (validated by http://jsonlint.com/)
Code: Select all
{
    "countries": [
        {
            "name": "Singapore",
            "tagLine": "",
            "cities": []
        },
        {
            "name": "Phillipines",
            "tagLine": "",
            "cities": [
                {
                    "name": "Pasay City",
                    "suburbs": []
                },
                {
                    "name": "Bay City",
                    "suburbs": []
                }
            ]
        },
        {
            "name": "Australia",
            "tagLine": "",
            "cities": [
                {
                    "name": "Sidney",
                    "suburbs": [
                        {
                            "name": "Paramatta"
                        }
                    ]
                },
                {
                    "name": "Perth",
                    "suburbs": [
                        {
                            "name": "Innaloo"
                        },
                        {
                            "name": "Canington"
                        },
                        {
                            "name": "Booragon"
                        }
                    ]
                }
            ]
        }
    ]
}

Hope this helps.
Hi KK,

That is just awesome, perfect solution. I honestly don't know how to thank you enough.

If you ever need any assistance with design, UI or CSS I am more than happy to help in anyway

Regards
Said
You are welcome, Said :)
7 posts Page 1 of 1

Who is online

In total there are 2 users online :: 0 registered, 0 hidden and 2 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 2 guests

cron