GopherSnippetsStar

Code snippets with tests and testable examples for the Go programming language

How to check if string is valid JSON

Snippets Index - Run code on Go playground - Edit

package main

import (
	"encoding/json"
	"fmt"
)

func Example() {
	valid := `{"foo":"bar"}`
	invalid := `}{`

	fmt.Println(json.Valid([]byte(valid)))
	fmt.Println(json.Valid([]byte(invalid)))
	// Output:
	// true
	// false
}

by psampaz - source code - comment below or here