https://github.com/bububa/ljson
parse loose json string
https://github.com/bububa/ljson
Last synced: 9 months ago
JSON representation
parse loose json string
- Host: GitHub
- URL: https://github.com/bububa/ljson
- Owner: bububa
- License: mit
- Created: 2025-02-15T05:44:47.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-10T07:17:44.000Z (over 1 year ago)
- Last Synced: 2025-03-10T08:25:34.144Z (over 1 year ago)
- Language: Go
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LJSON
ljson Go is a library that parse loose json string
---
[](https://pkg.go.dev/github.com/bububa/ljson)
[](https://github.com/bububa/ljson/actions/workflows/go.yml)
[](https://github.com/bububa/ljson/actions/workflows/goreleaser.yml)
[](https://github.com/bububa/ljson)
[](https://goreportcard.com/report/github.com/bububa/ljson)
[](https://github.com/bububa/ljson/blob/master/LICENSE)
[](https://GitHub.com/bububa/ljson/releases/)
## Install
Install the package into your code with:
```bash
go get "github.com/bububa/ljson"
```
Import in your code:
```go
import (
"github.com/bububa/ljson"
)
```
## Example
```go
package main
import (
"context"
"fmt"
"os"
"github.com/bububa/ljson"
)
func main() {
// Define the expected schema as a struct
type Nested struct {
A int `json:"a"`
}
type MySchema struct {
Field1 []map[string]string `json:"field1"`
NestedObj Nested `json:"nested"`
Numbers int `json:"numbers"`
BoolVal bool `json:"bool_val"`
}
// JSON with objects stored as strings and type mismatches
jsonStr := `{
"field1": "[{\"sub1\": \"xxx\"}, {\"sub2\": \"yyy\"}]",
"nested": "{\"a\": \"123\"}",
"numbers": "456",
"bool_val": "true"
}`
arrStr := `[{
"field1": "[{\"sub1\": \"xxx\"}, {\"sub2\": \"yyy\"}]",
"nested": "{\"a\": \"123\"}",
"numbers": "456",
"bool_val": "true"
}, {
"field1": "[{\"sub1\": \"xxx\"}, {\"sub2\": \"yyy\"}]",
"nested": "{\"a\": \"123\"}",
"numbers": "456",
"bool_val": "true"
}]`
mapStr := `"{\"sub1\": \"xxx\", \"sub2\": 123}"`
// Define a struct instance to receive the parsed data
var result MySchema
// Unmarshal using our loose parser
if err := ljson.Unmarshal([]byte(jsonStr), &result); err != nil {
return
}
// Print the processed result
resultJSON, _ := json.MarshalIndent(result, "", " ")
fmt.Println(string(resultJSON))
// Define a struct instance to receive the parsed data
var arrResult []MySchema
// Unmarshal using our loose parser
if err := l.Unmarshal([]byte(arrStr), &arrResult); err != nil {
return
}
// Print the processed result
resultJSON, _ = json.MarshalIndent(arrResult, "", " ")
fmt.Println(string(resultJSON))
// Define a struct instance to receive the parsed data
mapResult := make(map[string]string)
// Unmarshal using our loose parser
if err := l.Unmarshal([]byte(mapStr), &mapResult); err != nil {
return
}
// Print the processed result
resultJSON, _ = json.MarshalIndent(mapResult, "", " ")
fmt.Println(string(resultJSON))
// Example with interface type
interfaceData := `{
"some_field": "{\"a\": \"456\"}"
}`
var myInterface interface{}
if err := Unmarshal([]byte(interfaceData), &myInterface); err != nil {
fmt.Println("Error unmarshalling interface:", err)
} else {
fmt.Printf("Unmarshalled interface: %+v\n", myInterface)
}
```