REST / JSON formatting

Chantale Laberge asked on January 6, 2016 17:21

I'm trying to use the .json file created by the example at: https://docs.kentico.com/display/K8/Getting+data+using+REST

/<object type> 
/cms.country

But this page gives me an answer like:

{"cms_countries": 
[
{"cms_country": 
[
{"countrydisplayname":"Afghanistan"}
,{"countrydisplayname":"Albania"}
,{"countrydisplayname":"Algeria"}
... ... ... 
,{"countrydisplayname":"Zimbabwe"}
]
}
,{"TotalRecords": 
[
{"TotalRecords":"246"}
]
}
]
}

when all I need, will be something like:

{"countrydisplayname":"Afghanistan"}
,{"countrydisplayname":"Albania"}
,{"countrydisplayname":"Algeria"}
... ... ... 
,{"countrydisplayname":"Zimbabwe"}

SO, is there a way to get my data using REST formatted to give me this answer instead of the original one ?

(I meant, to get rid of "cms_countries" and "cms_country") I understand it will be possible also to parse this tree using other logic on my "typeahead" part, and use only the nodes I need, but... all tutorials on the web are pointing to the easy exemples :(

Thanks

Correct Answer

Chantale Laberge answered on January 11, 2016 16:18

Just in case, if someone needs to use typeahead with a Kentico .json file:

using a file like: {myKenticoSite}/rest/cms.country?format=json&columns=CountryName

(be aware... json can be case sensitive...)

<script type='text/javascript'> 
$(window).load(function(){
    var v01 = new Bloodhound({
        limit: 10,
        datumTokenizer: function (d) {
            return Bloodhound.tokenizers.whitespace(d.value);
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        prefetch: { 
            url: '{myKenticoSite}/rest/cms.country?format=json&columns=Countrydisplayname',
            filter: function (v01) {  
                return $.map(v01.cms_countries[0].cms_country, function (myCountry) {  
                    return {        
                        value: myCountry.Countrydisplayname 
                    };
                });
            }
        }
    });

    // Initialize the Bloodhound suggestion engine
    v01.initialize(); 

    // Instantiate the Typeahead UI
    $('.typeahead').typeahead(
        {
            hint: true,
            highlight: true,
            minLength: 1
        }, 
        {
        //displayKey: 'value',
        displayKey: function (f01) {
                  return f01.value;
              },
        source: v01.ttAdapter()
    });
}); 

</script> 


<div class="bs-example">
        <input class="typeahead">
</div>
0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on January 6, 2016 17:44

Not sure you can modify that rest result but I've used this before in my ajax calls and simply used something like this to select the json you want/need:

...
success: function (data) {
    var responseData = data.cms_countries[0].cms_country;
    ...
}
...
0 votesVote for this answer Mark as a Correct answer

Chantale Laberge answered on January 6, 2016 20:25

Thank Brenden, but I can't go further down the branches after the first element. Weird Json ! I'll post an answer if I came with another solution.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 6, 2016 21:23

Why can't you go further down the branches? Aren't you writing your own JS methods?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.