Wiki

Clone wiki

jsonij / Home

JSON in Java (JSONiJ) Wiki

Welcome to the main guide for the new version of JSONiJ. JSONiJ is a JSON Parser, JPath Implementation. Its easy to use and it has good performance.

Quickstart

Start here if you have a simple JSON job to do.

Install JSONiJ (Maven)

#!xml
<dependency>
    <groupId>cc.plural</groupId>
    <artifactId>jsonij</artifactId>
    <version>0.4.0</version>
</dependency>

Install JSONiJ (jar)

Download a release or a snapshot of JSONiJ and add it to your classpath.

Parse JSON String

Parsing a String of JSON is pretty straight forward;

#!java
JSON resultJSON = JSON.parse("{\"key\":[1,2.3,0.4,-5,-5.92,0.001E1,-0.045E45,987654321]}");

You can then traverse resultJSON similar to an XML DOM.

Parse JSON File

JSONiJ parses any JSON source wrapped in a java.io.InputStream and, once again, reading it in is pretty straight forward;

#!java

URL file = ClassLoader.class.getResource("/config.json");
JSON resultJSON = JSON.parse(file.openStream());

Pull Values From Parsed JSON

The most basic method to pull values out is using the Value API like the following;

#!java

Value jsonArray = resultJSON.get("key");

If you feel a little more adventurous then you can use JPath;

#!java

Value jsonArray = JPath.evaluate(resultJSON, "/key");

If you are on the run with JPath then why not just grab what you want, like the 3rd element of the key array;

#!java

Value jsonArray = JPath.evaluate(resultJSON, "/key[2]");

Sweet.

Releases

I always use low version numbers. Don't be put off by the fact that the version is not 1.X.Y+

Version

Version jsonij-0.4.0

Many changes over a long period.

  • The package has been removed from cc.plural.jsonij to simply jsonij.
  • Simplified the license to MIT.
  • Removed the Marshaller (it will be back somewhere - stay on 0.3.X if you need it).
  • Implement parsing for BSON.
  • JPath and JSON Parsing updates.

Older version notes can be found here.

Contact

I am happy to help out with any questions wherever I can. Simply contact me through j.w.marsden+jsonij@gmail.com or raise an issue.

Updated