Golang 2 min read

Parse SearchCans SERP JSON in Go and Test the Response Shape

A Go SERP client must decode the data object before reading the organic-result array.

(Updated: ) • 306 words

A Go SERP client must decode the data object before reading the organic-result array. In SearchCans, the result URL field is link. A struct that expects a top-level result list or a url field can compile while silently losing the data the application needs.

Make the nested JSON contract explicit

package main

import (
    "encoding/json"
    "fmt"
)

type Organic struct {
    Position int `json:"position"`
    Title string `json:"title"`
    Link string `json:"link"`
}
type Response struct {
    Code int `json:"code"`
    Data struct { Organic []Organic `json:"organic"` } `json:"data"`
}

func main() {
    raw := []byte(`{"code":0,"data":{"organic":[{"position":1,"title":"Fixture","link":"https://example.org/"}]}}`)
    var body Response
    if err := json.Unmarshal(raw, &body); err != nil { panic(err) }
    if body.Code != 0 { panic("business error") }
    for _, hit := range body.Data.Organic { fmt.Println(hit.Position, hit.Link) }
}

The envelope is an owned fixture. Check HTTP status before applying this parser to a real response, and keep a nonzero business code distinct from an empty organic list.

Test the client around the decoder

The accompanying Go client tests exercise the nested response with httptest. They verify a local contract, not search quality or provider throughput. Concurrency, cancellation and retry scheduling belong in the separate Go client guide.

  • Serve a nested success envelope from a local test server.
  • Assert the parsed link and position, not just the number of rows.
  • Add malformed JSON, an empty list and a business error to the application acceptance set.
  • Keep credentials in configuration rather than source code or logs.

Reference documentation: pkg.go.dev — /encoding/json; www.searchcans.com — /google-search-api.

Tags:

Golang Web Scraping Backend Engineering Concurrency Performance
SearchCans Team

SearchCans Team

SERP API & Reader API Experts

The SearchCans engineering team builds high-performance search APIs serving developers worldwide. We share practical tutorials, best practices, and insights on SERP data, web scraping, RAG pipelines, and AI integration.

Ready to build with SearchCans?

Test SERP API and Reader API with 100 free credits. No credit card required.