JSON

Learn how to use the JSON component.

This examples show how to parse JSON files using Camel.

For more details on using this library, refer to:

Library

Gson

To use JSON data format with Gson library, create a new library with the following jars (in this order):

Camel version Library jars
2.18.3

See How to retrieve the Camel version of Decision Insight.

 

Gson library deserializes JSON number to Java double. An alternative solution is to use Jackson library.

Jackson

To use JSON data format with Jackson library, create a new library with the following jars (in this order):

See How to retrieve the Camel version of Decision Insight.

Usage

JSON data sample

Example of JSON data from https://maps.googleapis.com :

Sample of a JSON file
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "eiffel tower",
               "short_name" : "eiffel tower",
               "types" : [ "premise" ]
            },
            {
               "long_name" : "Painted Horse Drive",
               "short_name" : "Painted Horse Dr",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Indian Trail",
               "short_name" : "Indian Trail",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vance",
               "short_name" : "Vance",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Union County",
               "short_name" : "Union County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "North Carolina",
               "short_name" : "NC",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "28079",
               "short_name" : "28079",
               "types" : [ "postal_code" ]
            }
         ],
         
........

   "status" : "OK"
}

Route

To unmarshal data using Gson library, create and start this routing context:

<routes xmlns="http://camel.apache.org/schema/spring" xmlns:u="http://www.systar.com/aluminium/camel-util">
  <route>
    <from uri="timer:single?repeatCount=1"/>
    <to uri="https4://maps.googleapis.com/maps/api/geocode/json?address=Eiffel+Tower"/>
    <unmarshal>
      <json library="Gson" unmarshalTypeName="java.lang.Object"/>
    </unmarshal>
    <log message="${body[status]}"/>
    <split>
      <simple>${body[results]}</simple>
      <split>
        <simple>${body[address_components]}</simple>
        <log message="${body}"/>
      </split>
    </split>
  </route>
</routes>

To use Jackson library, simply change the library attribute in previous routing context:

(...)
    <unmarshal>
      <json library="Jackson" unmarshalTypeName="java.lang.Object"/>
    </unmarshal>
(...)

Results 

2016-02-08 19:21:35,325 [Camel (dataintegration.EiffelTower) thread #92 - timer://single] INFO route1 - OK
2016-02-08 19:21:35,333 [Camel (dataintegration.EiffelTower) thread #92 - timer://single] INFO route1 - {long_name=eiffel tower, short_name=eiffel tower, types=[premise]}
2016-02-08 19:21:35,340 [Camel (dataintegration.EiffelTower) thread #92 - timer://single] INFO route1 - {long_name=Painted Horse Drive, short_name=Painted Horse Dr, types=[route]}
2016-02-08 19:21:35,357 [Camel (dataintegration.EiffelTower) thread #92 - timer://single] INFO route1 - {long_name=Indian Trail, short_name=Indian Trail, types=[locality, political]}
2016-02-08 19:21:35,358 [Camel (dataintegration.EiffelTower) thread #92 - timer://single] INFO route1 - {long_name=Vance, short_name=Vance,types=[administrative_area_level_3, political]}
2016-02-08 19:21:35,358 [Camel (dataintegration.EiffelTower) thread #92 - timer://single] INFO route1 - {long_name=Union County, short_name=Union County, types=[administrative_area_level_2, political]}
...

Related Links