Skip to main content

JSON Parser

Parses JSON data. See Supported reader-parser combinations for compatible readers.

JSON Parser properties

property

type

default value

comments

Event Type

String

the Striim data type to be used (leave blank to parse manually)

Field Name

String

if the JSON includes fields, specify the one containing the events defined by eventType (see example below)

The output type of a source using JSONParser is JSONNodeEvent.

JSON Parser example

Assume that the JSON being parsed has the following format:

{
    "ConnectedToWifi": false,
    "IpAddress": "",
    "WifiSsid": "",
    "device": "LGE Nexus 5",
    "OsVersion": "21",
    "platfrom": "Android",
    "scanresult": {
        "timestamp1": "1424434411",
        "rssi": -90
    },
    "serviceUUID": "8AA10000-0A46-115F-D94E-5A966A3DDBB7",
    "majorId": 15,
    "minorId": 562
}

The following code extracts the timestamp1 and rssi properties from the scanresult field:

CREATE TYPE ScanResultType (
  timestamp1 String,
  rssi String
);
CREATE STREAM ScanResultStream OF ScanResultType;

CREATE  SOURCE JSONSource USING FileReader (
  directory: 'Samples',
  WildCard: 'sample.json',
  positionByEOF: false
)
PARSE USING JSONParser (
  eventType: 'ScanResultType',
  fieldName: 'scanresult'
)
OUTPUT TO ScanResultStream;

In the UI, when creating a source that uses the JSON Parser, create the output stream first, specifying a data type corresponding to the fields in the JSON data, then enter the name of that data type as the value for the eventType property in the source.

If the data you need is spread among two or more JSON fields, you can parse the file manually by leaving eventType blank. For example, assume your JSON had this format:

{
    "venue": {
        "lat": 41.773228,
        "lon": -88.149109,
        "venue_id": 23419382,
        "venue_name": "Naperville Theater"
    },
    "event": {
        "event_id": "418361985",
        "event_name": "Naperville Film Festival"
    },
    "group": {
        "group_city": "Naperville",
        "group_state": "IL",
        "group_country": "us",
        "group_id": 8625752,
        "group_name": "NFF"
    }
  }

The following code gets properties from all three fields:

CREATE SOURCE RawMeetupJSON USING FileReader (
  directory:'./Samples/meetup',
  wildcard:'one_event_pretty.json',
  positionByEOF:false
) 
PARSE USING JSONParser (
  eventType:''
)
OUTPUT TO RawJSONStream;
 
CREATE TYPE MeetupJSONType (
  venue_id string KEY,
  group_name string,
  event_name string,
  venue_name string,
  group_city string,
  group_country string,
  lat double,
  lon double
);
CREATE STREAM ParsedJSONStream of MeetupJSONType;
 
CREATE CQ ParseJSON
INSERT INTO ParsedJSONStream
SELECT
  data.get('venue').get('venue_id').textValue(),
  data.get('group').get('group_name').textValue(),
  data.get('event').get('event_name').textValue(),
  data.get('venue').get('venue_name').textValue(),
  data.get('group').get('group_city').textValue(),
  data.get('group').get('group_country').textValue(),
  data.get('venue').get('lat').doubleValue(),
  data.get('venue').get('lon').doubleValue()
FROM RawJSONStream 
WHERE data IS NOT NULL;
CREATE TARGET MeetupJSONOut USING SysOut(name:meetup) INPUT FROM ParsedJSONStream;

The output for the JSON shown above would be as follows:

meetup: MeetupJSONType_1_0{
  venue_id: null
  group_name: "OpenHack Naperville"
  event_name: "February Hack Night!!"
  venue_name: "Twocanoes Software office (UPSTAIRS above Costellos Jewelry)"
  group_city: "Naperville"
  group_country: "us"
  lat: 41.773228
  lon: -88.149109
};