Skip to content

Commit

Permalink
📚 docs: update README.md about feat: read json dataset from file #4 #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jan 20, 2025
1 parent a77b432 commit f7bb2d6
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,37 @@ func main() {
}
```
### Read JSON by File
If you're working with the JSON dataset stored in a file (with the extension `.json` or `.txt`), you can use the `ParseBufio(in io.Reader)` function to read all the content of the JSON into the `Context` struct.
eg.
```go
package main
import (
"fmt"
"os"
"github.com/sivaosorg/fj"
)
func main() {
file, err := os.Open("./logs/data.json")
if err != nil {
return
}
defer file.Close()
ctx := fj.ParseBufio(file)
value := ctx.Get(`stock.0.symbol`)
fmt.Println(value.String()) // MMM
value = ctx.Get(`bank.0.@minify`)
fmt.Println(value.String()) // {"isActive":false,"balance":"$1,404.23","age":26,"eyeColor":"blue","name":"Stark Jenkins","gender":"male","company":"HINWAY","email":"[email protected]","phone":"+1 (943) 542-3591","address":"766 Cooke Court, Dunbar, Connecticut, 9512"}
}
```
### Validation
The `Get*` and `Parse*` functions assume that the JSON is properly structured. Invalid JSON won't cause a panic, but it could lead to unexpected outcomes.
Expand Down

0 comments on commit f7bb2d6

Please sign in to comment.