You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@metamodel.apache.org by Joao Boto <bo...@boto.pro> on 2014/11/07 16:41:30 UTC

complex json file

i'm trying to understand how to read a complex json file..

i started with this json (but will work with a much more complex):
{
"name":{ "first":"Joe", "last":"Sixpack" },
"gender":"MALE",
"verified":false,
"userImage":"Rm9vYmFyIQ=="
}

trying to do what Kasper told me on
https://issues.apache.org/jira/browse/METAMODEL-38

I wrote the code below, but i can't get "first" and "last" fields..

SimpleTableDef custTable = new SimpleTableDef(
"customer",
new String[] {"name.first","name.last","gender","verified","userimage"}
);

SchemaBuilder schema = new SimpleTableDefSchemaBuilder("tester", custTable)
;
JsonDataContext dc = new JsonDataContext(new FileResource(new
File("src/test/resources/datafeed.json")),schema);
Table table = dc.getDefaultSchema().getTableByName("customer");
Column firstName = table.getColumnByName("name.first");
Column lastName = table.getColumnByName("name.last");
Column gender = table.getColumnByName("gender");
DataSet dataSet =
dc.query().from(table).select(firstName,lastName,gender).execute();
while (dataSet.next()) {
String sFirstName = (String) dataSet.getRow().getValue(firstName);
String sLastName = (String) dataSet.getRow().getValue(lastName);
String sGender = (String) dataSet.getRow().getValue(gender);
}

can someone tell me what I'm doing wrong or indicate which way to go?

thanks

Best regards

Re: complex json file

Posted by Kasper Sørensen <i....@gmail.com>.
Hi Joao,

I just double double checked and it turned out I was wrong :-/ I was
thinking of a capability that we have in some of the other DataContext
implementations (MongoDb I think) to make such column names that represent
nested fields. But I can see that this does not work for JSON in our
current implementation.

That is however an improvement I feel is quite important. So I've
registered an issue for it [1] at least ...

In the meantime, the way forward is to get "name" and then cast the value
to a Map and get "first" and "last" from that map.

Best regards,
Kasper

[1] https://issues.apache.org/jira/browse/METAMODEL-92

2014-11-07 16:41 GMT+01:00 Joao Boto <bo...@boto.pro>:

> i'm trying to understand how to read a complex json file..
>
> i started with this json (but will work with a much more complex):
> {
> "name":{ "first":"Joe", "last":"Sixpack" },
> "gender":"MALE",
> "verified":false,
> "userImage":"Rm9vYmFyIQ=="
> }
>
> trying to do what Kasper told me on
> https://issues.apache.org/jira/browse/METAMODEL-38
>
> I wrote the code below, but i can't get "first" and "last" fields..
>
> SimpleTableDef custTable = new SimpleTableDef(
> "customer",
> new String[] {"name.first","name.last","gender","verified","userimage"}
> );
>
> SchemaBuilder schema = new SimpleTableDefSchemaBuilder("tester", custTable)
> ;
> JsonDataContext dc = new JsonDataContext(new FileResource(new
> File("src/test/resources/datafeed.json")),schema);
> Table table = dc.getDefaultSchema().getTableByName("customer");
> Column firstName = table.getColumnByName("name.first");
> Column lastName = table.getColumnByName("name.last");
> Column gender = table.getColumnByName("gender");
> DataSet dataSet =
> dc.query().from(table).select(firstName,lastName,gender).execute();
> while (dataSet.next()) {
> String sFirstName = (String) dataSet.getRow().getValue(firstName);
> String sLastName = (String) dataSet.getRow().getValue(lastName);
> String sGender = (String) dataSet.getRow().getValue(gender);
> }
>
> can someone tell me what I'm doing wrong or indicate which way to go?
>
> thanks
>
> Best regards
>