0

This question is related to this solved one (i.e. similar code but it is a different question. The output of the code below is a series of json objects, but taken together they are not json (at least in the format that I need it). i.e. The objects are not in an array, and there is not a comma between them. How can I return comma separated objects in an array?

buffer := new(bytes.Buffer)
for _, jsonRawMessage := range sliceOfJsonRawMessages{
    if err := json.Compact(buffer, jsonRawMessage); err != nil{
        fmt.Println("error")

    }

}
output, _ := json.Marshal(buffer.String())
w.Header().Set("Content-Type", contentTypeJSON)

w.Write(output)

Output (2 distinct json objects but there's actually a lot more)

{
    "Dir": "/usr/local/go/src/bytes",
    "ImportPath": "bytes",
    "Name": "bytes",
    "Doc": "Package bytes implements functions for the manipulation of byte slices.",
    "Target": "/usr/local/go/pkg/darwin_amd64/bytes.a",
    "Goroot": true,
    "Standard": true,
    "Root": "/usr/local/go",
    "GoFiles": [
        "buffer.go",
        "bytes.go",
        "bytes_decl.go",
        "reader.go"
    ],
    "IgnoredGoFiles": [
        "equal_test.go"
    ],
    "Imports": [
        "errors",
        "io",
        "unicode",
        "unicode/utf8"
    ],
    "Deps": [
        "errors",
        "io",
        "runtime",
        "sync",
        "sync/atomic",
        "unicode",
        "unicode/utf8",
        "unsafe"
    ],
    "TestGoFiles": [
        "export_test.go"
    ],
    "XTestGoFiles": [
        "buffer_test.go",
        "bytes_test.go",
        "compare_test.go",
        "example_test.go",
        "reader_test.go"
    ],
    "XTestImports": [
        "bytes",
        "encoding/base64",
        "fmt",
        "io",
        "io/ioutil",
        "math/rand",
        "os",
        "reflect",
        "runtime",
        "sort",
        "sync",
        "testing",
        "unicode",
        "unicode/utf8"
    ]
}{
    "Dir": "/usr/local/go/src/errors",
    "ImportPath": "errors",
    "Name": "errors",
    "Doc": "Package errors implements functions to manipulate errors.",
    "Target": "/usr/local/go/pkg/darwin_amd64/errors.a",
    "Goroot": true,
    "Standard": true,
    "Root": "/usr/local/go",
    "GoFiles": [
        "errors.go"
    ],
    "Deps": [
        "runtime",
        "unsafe"
    ],
    "XTestGoFiles": [
        "errors_test.go",
        "example_test.go"
    ],
    "XTestImports": [
        "errors",
        "fmt",
        "testing",
        "time"
    ]
}
3
  • Can you add what the content, exactly, sliceOfJsonRawmessages is? Commented Mar 24, 2015 at 17:31
  • @Momer I create a slice that takes json.RawMessage and then append jso.RawMessage (s) to it, those json.RawMessages are exactly what you see in the OP Commented Mar 24, 2015 at 18:35
  • Never ignore errors! Commented Mar 24, 2015 at 20:18

1 Answer 1

3

You can concatenate them yourself:

var buff bytes.Buffer
buff.WriteByte('[')

for i, j := range jsons {
    if i != 0 {
      buff.WriteByte(',')
    }
    buff.Write([]byte(j))
}
buff.WriteByte(']')

you can then use json.Indent or json.Compact if you need to clean up the json further.

var output bytes.Buffer
err = json.Indent(&output, buff.Bytes(), "", "  ")
// or json.Compact(&output, buff.Bytes())
if err != nil {
    // something wrong with the json
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but you can't pass buff to json.Compact as a source. Could you add a little more detail?
@Leahcim: you use the Bytes() method to get a slice from the buffer
after I do json.Compact or json.Indent i.e. if err := json.Compact(&output, buff.Bytes()); err != nil{ fmt.Println("error") } , I then do output, _ := json.Marshall(output.String()) and return output w.Write(output), however, when I try to access to first object of the array i.e.result[0] in the success callback of a (javascript) ajax call, I get the left bracket [ rather than the first object inside the left bracket. Same if I do json.Indent. Can you tell me what I'm doing wrong? (btw, thank you so much for your help)
@Leahcim: the contents of the output buffer are already marshaled. Marshaling output.String() writes it in a single json string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.