Forum Discussion

charlesssgraham's avatar
charlesssgraham
Contributor
3 years ago

Access LiQL JSON with JavaScript?

So the LiQL queries give us back a JSON object. I'd like to just work with the results of the query with the code I write in a <script> tag on the same component. 

Is this possible in Khoros? 

    • charlesssgraham's avatar
      charlesssgraham
      Contributor

      jeffshurtliff Thanks for the reply! I'm a bit stuck on how to translate the following query into a custom endpoint: 

      "SELECT id, login, email, rank.name, rank.simple_criteria.role.id FROM users"

      The example given in the docs is a bit simplistic.. Could you give me a pointer or two on how to convert the above LiQL? After the endpoint is constructed, do I call it between the <@liaAddScript> tags or do I pass it into a fetch( )? 

      Thanks!

      • JavidH's avatar
        JavidH
        Khoros Staff

        charlesssgraham You can add your query ("SELECT id, login, email, rank.name, rank.simple_criteria.role.id FROM users")  to a custom component in studio using the below API V2:

         

         

        <#assign query = restadmin("2.0","/search?q=" + "SELECT id, login, email, rank.name, rank.simple_criteria.role.id FROM users"?url).data.items/>
        
        <#list query as q>
            ${q.id}
            ${q.login}
            ${q.email}
            ${q.rank.name}
            <#if q.rank.simple_criteria.role?? && q.rank.simple_criteria.role.id??>
            ${q.rank.simple_criteria.role.id}
        </#if>,
        </#list>

         

        Then you can add this custom component to your desired page in your community in studio.

        Hope this helps.

  • charlesssgraham If any of the comments were helpful, would you mind marking it as an accepted solution as it may help other users who may have similar requests?

    • charlesssgraham's avatar
      charlesssgraham
      Contributor

      Done! One more question: is there an equivalent of logging query results to the console? 

      • JavidH's avatar
        JavidH
        Khoros Staff

        Create a custom endpoint (I'm naming it as q) in the studio:

         

        <#assign query= restadmin("2.0","/search?q=" + "SELECT id, login, email, rank.name, rank.simple_criteria.role.id FROM users"?url).data.items/>
        
        ${query}

         

         

        Create a custom component (naming it as component_q) in the studio:

         

        <#assign communityId = community.id />
        <#assign communityUrl = http.request.url />
        <#assign lengthOfDomain = communityUrl?index_of("/") />
        <#assign communityDomain = communityUrl?substring(0,lengthOfDomain) />
        ${communityUrl}
        ${communityId}
        <@liaAddScript>
        ;(function($){
                        $.ajax({ 
                            type:"POST",                 
                            url:'${communityDomain}/plugins/custom/qa/johndowden/q',
                            contentType: 'application/json',
                            success: function(res) {
                                console.log(res);
                                console.log("Results are listed"); 
                            }.bind(this),
                            error: function(xhr, status, err) {
                                console.error(xhr, status, err.toString());
                                console.log("Unable to view the results");
                            }.bind(this)
                        });
        })(LITHIUM.jQuery);
        </@liaAddScript>

         

        Note: The URL must be your community URL of the endpoint in the studio.

         

        When you add the component_q to any page in your community using studio, you can view the results in the console.

        Hope this helps.

        If this answers your question, would you please mark it as an accepted solution.