Som
Partner

Redering multiple json objects

 

 

 

 

Problem statement:

We are experiencing some issues with rendering multiple json objects on a single dynamic page.

Below is the sample format of what we are trying to achieve:

 

{
"title": "",
"articles": [
{
"id" : ""
"heading":"",
"publishedDate":"",
"categories":"",
"articleImage":"",
"description":""
},
{
"id" : ""
"heading":"",
"publishedDate":"",
"categories":"",
"articleImage":"",
"description":""
}
]

}

 

Regards,

Somnath

 

0 Kudos
3 Replies
MarcusEdwards
Crownpeak (Retired)

You don't say what issues you're having so it is a little hard to help.

However, let's assume you're trying to get this JSON from Search G2 rather than from a publish JSON resource, and look at the basics.

You should be able to get a list of articles from G2 with JSON that looks like this in the "docs" field of the G2 response object:

GET https://searchg2.crownpeak.net/{collection}/select?q=*&fq=custom_s_type:article...

[
    {
        "id": "...",
        "title": "...",
        "custom_dt_publishedDate": "...",
        "custom_ss_categories": [
            "...",
            "...",
            "..."
        ],
        "custom_s_articleImage": "...",
        "custom_s_description": "..."
    },
    {
        "id": "...",
        "title": "...",
        "custom_dt_publishedDate": "...",
        "custom_ss_categories": [
            "...",
            "...",
            "..."
        ],
        "custom_s_articleImage": "...",
        "custom_s_description": "..."
    }
]

 

To get this into a format that matches your requirement, use Solr field aliases:

GET https://searchg2.crownpeak.net/{collection}/select?q=*&fq=custom_s_type:article&fl=heading:title,publishDate:custom_dt_publishDate,categories:custom_ss_categories,articleImage:custom_s_articleImage,description:custom_s_description...

 

[
    {
        "id": "...",
        "heading": "...",
        "publishedDate": "...",
        "categories": [
            "...",
            "...",
            "..."
        ],
        "articleImage": "...",
        "description": "..."
    },
    {
        "id": "...",
        "heading": "...",
        "publishedDate": "...",
        "categories": [
            "...",
            "...",
            "..."
        ],
        "articleImage": "...",
        "description": "..."
    }
]

 

Based on the JSON sample you provided, you will need to construct the JSON object with the top-level "title" and "articles" elements in your G2 response handling code.

The benefit of this approach is that the list of articles is dynamic and discovered/populated by query rather than having to fix the content at the point of publishing.

0 Kudos

How do we implement to create a JSON or output.aspx with multiple pages (like  Pages - Article 1, Article 2, Artilce 3,..... so on which references the same input,output and Preview files but it should generete the different articles in the JSON Objects).

0 Kudos

The typical way to do this is in two parts: 1) content creation; and 2) content presentation.

Part 1, requires you have a template for article assets. This template will have all the usual handlers (input, preview etc.) but should also have the G2 handlers (search_g2_insert.aspx etc.).

Once you have the template, content editors can create articles and publish these using workflow. The template handlers will create the JSON and publish this to Search G2.

Part 2 is where your application queries Search G2 for a list of articles. This query could be for all articles but more likely will be looking for specific articles -- the 10 most recent articles, or articles tag with category "news" etc. 

0 Kudos