Output Nested JSON

Hello;
I’m trying to use hugo to output data that i get from external API call.

I want to have an output as follow:

    {
       "157": "CATALOGUE CADEAUX", 
        "subcategories":{
                    "2": "Accueil",
                    "219": "Vente"}
    }

at the moment i cannot do this using this code:

    {{ $cats :=  getJSON $mainUrl $id "?io_format=JSON" }}
    {{ $finalcats := (dict $id $cats.category.name) }}
    {{ range $cats.category.associations.categories }}
          {{ $subcats :=  getJSON $mainUrl .id "?io_format=JSON" }}
          {{ $strId := printf "%d" (int $subcats.category.id)}}
          {{ $finalcats = merge $finalcats (dict $strId $subcats.category.name) }}
    {{ end }}
    {{ $finalcats|jsonify (dict "prefix" " " "indent" "  ")}}

That outputs:

{
   "157": "CATALOGUE CADEAUX", 
   "2": "Accueil",
   "219": "Vente"
}

Any one can help please?
Regards

You need to show the input JSON -the one that you are fetching with getJSON- otherwise it is not possible to answer your question.

Hi, thank you for response.
Here is a sample of a JSON describing a category

    {
    "category": {
        "id": 2,
        "id_parent": "1",
        "level_depth": "1",
        "nb_products_recursive": "4436",
        "active": "1",
        "id_shop_default": "1",
        "is_root_category": "1",
        "position": "1",
        "date_add": "2013-08-04 13:37:03",
        "date_upd": "2016-10-03 13:23:31",
        "name": "Accueil",
        "link_rewrite": "home",
        "description": null,
        "meta_title": null,
        "meta_description": null,
        "meta_keywords": null,
        "associations": {
            "categories": [
                {
                    "id": "4"
                },
                {
                    "id": "3"
                },
                {
                    "id": "43"
                },
                {
                    "id": "6"
                },
                {
                    "id": "8"
                },
                {
                    "id": "219"
                },
                {
                    "id": "157"
                },
                {
                    "id": "229"
                },
                {
                    "id": "250"
                },
                {
                    "id": "251"
                },
                {
                    "id": "252"
                },
                {
                    "id": "253"
                },
                {
                    "id": "254"
                },
                {
                    "id": "255"
                }
            ]
        }
    }
}

Regards

You do not need to fetch the remote JSON again when within the scope of
range $cats.category.associations.categories

You need to construct your template to output what you need by referencing the remote keys and their values within the desired subcategories output. Something like:

"subcategories":{
 "id": {{ $strId }}
}

And so on…

Also for a generic article about building an API with Hugo have a look at:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.