Remove Scratch from list

I have the following code rendering a .json file for a section.

{{- $.Scratch.Add "index" slice -}}
{{- range .Data.Pages -}}
{{- $.Scratch.Add "index" (dict "objectID" ( printf "issue_%s" .Slug ) "issue" ( int .Slug ) "uri" .Permalink "title" .Title "blurb" ( .Params.description | plainify ) "tags" .Params.tags) -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

Currently it outputs all of the separate dict’s inside a list, eg:

[
  {
    "blurb": "lah",
    "issue": 1,
    "objectID": "issue_1",
    "tags": [
      "some tag"
    ],
    "title": "some title",
    "uri": "http://example.com/1/"
  },
  {
    "blurb": "lah",
    "issue": 2,
    "objectID": "issue_2",
    "tags": [
      "some tag"
    ],
    "title": "some title",
    "uri": "http://example.com/2/"
  }
]

Is there a way to remove all the items from inside the list and just have raw dicts, eg:

{
  "blurb": "lah",
  "issue": 1,
  "objectID": "issue_1",
  "tags": [
    "some tag"
  ],
  "title": "some title",
  "uri": "http://example.com/1/"
},
{
  "blurb": "lah",
  "issue": 2,
  "objectID": "issue_2",
  "tags": [
    "some tag"
  ],
  "title": "some title",
  "uri": "http://example.com/2/"
}

I’m not sure the last example is even valid JSON …?

But, you might have a look at Scratch’s SetInMap, which conceptually may come closer to what you look for.

heh, yeah you’re totally right. Turns out what I actually wanted to do was this:

{{- $.Scratch.Add "index" slice -}}
{{- range .Data.Pages -}}
  {{- $.Scratch.Add "index" (dict "action" "addObject" "body" ( dict "objectID" ( printf "issue_%s" .Slug ) "issue" ( int .Slug ) "uri" .Permalink "title" .Title "blurb" ( .Params.description | plainify ) "tags" .Params.tags) ) -}}
{{- end -}}
{{- $list := $.Scratch.Get "index" -}}
{{- (dict "requests" $list ) | jsonify -}}

Which wasn’t even anything like the example I gave. Thanks for trying to help me and sorry for wasting your time!

1 Like