/jsparser

JSON Stream parser for GO

Primary LanguageGoMIT LicenseMIT

jsparser

jsparser is a json parser for GO. It is efficient to parse large json data with streaming fashion.

Usage

{
  "books": [{
      "title": "The Iliad and The Odyssey",
      "price": 12.95,
      "comments": [{
        "rating": 4,
        "comment": "Best translation I've read."
      }, {
        "rating": 2,
        "comment": "I like other versions better."
      }]
    },
    {
      "title": "Anthology of World Literature",
      "price": 24.95,
      "comments": [{
        "rating": 4,
        "comment": "Excellent overview of world literature."
      }, {
        "rating": 3,
        "comment": "Needs more modern literature."
      }]
    }
  ]
}

Stream over books

f, _ := os.Open("input.json")
br := bufio.NewReaderSize(f,65536)
parser := jsparser.NewJSONParser(br, "books")

for json:= range parser.Stream() {
		fmt.Println(json.ObjectVals["title"])
		fmt.Println(json.ObjectVals["price"])
		fmt.Println(json.ObjectVals["comments"].(*jsparser.JSON).ArrayVals[0].(*jsparser.JSON).ObjectVals["rating"])
}

// for relatively small size json. get all the results as slice
for json:= range parser.Parse() {
}

Skip props for efficiency

parser := pr.NewJSONParser(br, "books").SkipProps([]string{"comments", "price"})  

Error handling

for json:= range parser.Stream() {
    if json.Err !=nil {
      // handle error
    }
}

Progress of parsing

// total byte read to calculate the progress of parsing
parser.TotalReadSize

If you are interested check also xml parser which works similarly.