JSON Extractor is a Go module for extracting valid JSON objects from anywhere, It is also used in the yak.
go get -u github.com/WAY29/jsonextractor
package main
import (
"fmt"
"github.com/WAY29/jsonextractor"
)
func main() {
// fix invalid json escape
str := `{"message": "This is not a valid JSON string \x0A"}`
fixed, ok := jsonextractor.FixJson([]byte(str))
fmt.Println(string(fixed)) // Output: {"message": "This is not a valid JSON string \u000a"}
fmt.Println(ok) // Output: true
fixed, ok = FixJson([]byte(`{"abc": 123,}`))
fmt.Println(string(fixed)) // Output: {"abc": 123}
fmt.Println(ok) // Output: true
}
package main
import (
"fmt"
"github.com/WAY29/jsonextractor"
)
func main() {
str := `// something trush...
<html> <!-- something html-->
{"name": "John", "age": 25}{"name": "Jim", "age": 30}</html>`
results, _, err := jsonextractor.ExtractJSONWithRaw(str)
if err != nil {
panic(err)
}
for _, r := range results {
fmt.Println(r)
}
// Output:
// {"name": "John", "age": 25}
// {"name": "Jim", "age": 30}
// or use ExtractStandardJSON
results2 := jsonextractor.ExtractStandardJSON(str)
for _, r := range results2 {
fmt.Println(r)
}
// Output:
// {"name": "John", "age": 25}
// {"name": "Jim", "age": 30}
}
The module is licensed under the MIT license.
Original author: VillanCh