You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by Apache Wiki <wi...@apache.org> on 2014/07/23 13:56:02 UTC

[Metamodel Wiki] Update of "examples/JsonDataContext" by KasperSorensen

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Metamodel Wiki" for change notification.

The "examples/JsonDataContext" page has been changed by KasperSorensen:
https://wiki.apache.org/metamodel/examples/JsonDataContext

New page:
= JSON DataContext - MetaModel module for reading JSON files =

As of MetaModel version 4.2 there is a module called MetaModel-json that allows you to explore and fire queries on a file containing JSON documents.

JSON documents may be appended together, or formatted in the form of a JSON array. For instance

{{{
{"id":1234, "name":"Foo"}
{"id":1235, "name":"Bar"}
{"id":1236, "name":"Baz"}
}}}

Or:

{{{
[
{"id":1234, "name":"Foo"},
{"id":1235, "name":"Bar"},
{"id":1236, "name":"Baz"}
]
}}}

== Example ==

Here's an example code use of a JSON file:

{{{#!java
JsonDataContext dc = new JsonDataContext(new File("src/test/resources/array_with_documents.json"));
Table table = dc.getDefaultSchema().getTable(0);
DataSet dataSet = dc.query().from(table).select("id", "name").where("id").gt(1000).execute();
while (dataSet.next()) {
  Number id = (Number) dataSet.getRow().getValue(0);
  String name = (String) dataSet.getRow().getValue(1);
  // do something with the names and IDs
}
dataSet.close();
}}}