Parsing multiple JSON objects in Go

golang parse json response
golang parse json array
golang unmarshal unknown json
golang json decode vs unmarshal
golang print json
golang read json file
golang json library
go parse string as json

Objects like the below can be parsed quite easily using the encoding/json package.


The trouble I am facing is when there are multiple dicts returned from the server like this :


This can't be parsed using the code below.

correct_format := strings.Replace(string(resp_body), "}{", "},{", -1)
json_output := "[" + correct_format + "]"

I am trying to parse Common Crawl data (see example).

How can I do this?

Assuming your input is really a series of valid JSON documents, use a json.Decoder to decode them:

package main

import (

var input = `
{"foo": "bar"}
{"foo": "baz"}

type Doc struct {
    Foo string

func main() {
    dec := json.NewDecoder(strings.NewReader(input))
    for {
        var doc Doc

        err := dec.Decode(&doc)
        if err == io.EOF {
            // all done
        if err != nil {

        fmt.Printf("%+v\n", doc)


If your input really is what you've shown in the question, that's not JSON and you have to write your own parser.

How to read multiple JSON objects from one file in Go, To do this, you need to wrap the Mysql and Postgres structure into a Configuration structure then pass it to Unmarshal function: JSON is used as the de-facto standard for data serialization, and by the end of this post, you’ll get familiar with how to parse and encode JSON in Go. In this post: 1. Parsing JSON strings; 2. Structured data (Decoding JSON to Structs) • JSON Arrays • Embedded objects • Primitives • Custom attribute names; 3. Unstructured data (Decoding JSON to maps) 4. Encoding JSON from Go data • Encoding structured data • Ignoring Empty Fields • Encoding Arrays and Slices • Encoding

Seems like each line is its own json object.

You may get away with the following code which will structure this output into correct json:

package main

import (

func main() {
    base := `{"trolo":"lolo"}

    delimited := strings.Replace(base, "\n", ",", -1)

    final := "[" + delimited + "]"

You should be able to use encoding/json library on final now.

How to Parse JSON in Golang (With Examples), Parsing JSON strings; 2. Structured data (Decoding JSON to Structs); • JSON Arrays; • Embedded objects; • Primitives; • Custom attribute names  Parsing JSON in go is very each and its depends upon developer preferences whether they want to use struct or they want to parse them raw using interfaces. Will keep posting new articles! Thank you!!!

Another option would be to parse each incoming line, line by line, and then add each one to a collection in code (ie a slice) Go provides a line scanner for this.

yourCollection := []yourObject{}
scanner := bufio.NewScanner(YOUR_SOURCE)
for scanner.Scan() {
    obj, err := PARSE_JSON_INTO_yourObject(scanner.Text())
    if err != nil {
       // something
    yourCollection = append(yourCollection, obj)
if err := scanner.Err(); err != nil {
    fmt.Fprintln(os.Stderr, "reading standard input:", err)

How to use JSON with Go [best practices] · YourBasic Go, Here we will see how we can parse JSON Object and Array using GoLang Interfaces. This reduce overhead to creating struct when data is unstructured and we  Let’s try and read in a simple JSON file and then parse it. For the purpose of this tutorial we’ll be parsing the following json within our file. Copy this and save it into a users.json file within the same directory as your main.go file.

GoLang : Dynamic JSON Parsing using empty Interface and without , Marshaling data structures into JSON with multiple types of message bodies with a If you just ask to unmarshal the above JSON objects into the Envelope type  To Load and parse a JSON file with multiple JSON objects we need to follow below steps: Create an empty list called jsonList; Read the file line by line because each line contains valid JSON. i.e., read one JSON object at a time. Convert each JSON object into Python dict using a json.loads() Save this dictionary into a list called result jsonList.

Dynamic JSON in Go, Let's say that you're building a JSON API with Go. Unmarshal() , which would return an error if the body contains multiple JSON objects. A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with JSON.parse(), and the data becomes a JavaScript object.

How to Parse a JSON Request Body in Go, An introduction to parsing JSON using the Go programming language (golang). If you're not familiar an empty interface{} is a way of defining a variable in Go  Using go to unmarshal json lists with multiple types. (JSON objects within or a 3rd-party library is going to become the "real" JSON parsing library for when

  • It is not a valid json that's the reason first convert it into valid json and then parse it.
  • That JSON cannot be parsed at all, let alone easily, because it's invalid.
  • Json is not invalid. Each line contains a separate Json object actually.
  • Note that the document at the URL in the question contains a text file with line-delimited JSON (i.e. one JSON document per line).
  • Sorry, by mistake i posted wrong json