Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/way29/jsonextractor
Go module for extracting valid JSON objects from anywhere
https://github.com/way29/jsonextractor
Last synced: 12 days ago
JSON representation
Go module for extracting valid JSON objects from anywhere
- Host: GitHub
- URL: https://github.com/way29/jsonextractor
- Owner: WAY29
- Created: 2023-03-17T04:15:46.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-17T04:47:15.000Z (almost 2 years ago)
- Last Synced: 2024-11-07T19:52:56.138Z (2 months ago)
- Language: Go
- Size: 4.88 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JSON Extractor
JSON Extractor is a Go module for extracting valid JSON objects from anywhere, It is also used in the [yak](https://github.com/yaklang/yakit).## Installation
```bash
go get -u github.com/WAY29/jsonextractor
```## Usage
### Fix JSON
```go
package mainimport (
"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
}
```### Extract JSON Objects
```go
package mainimport (
"fmt""github.com/WAY29/jsonextractor"
)func main() {
str := `// something trush...
{"name": "John", "age": 25}{"name": "Jim", "age": 30}`
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}
}
```## License
The module is licensed under the MIT license.## Thanks
Original author: [VillanCh](https://github.com/VillanCh)