BookmarkSubscribeRSS Feed

Reading JSON messages with SAS ESP

Started ‎03-22-2017 by
Modified ‎02-16-2018 by
Views 2,728

If REST is the new SOAP, then JSON is definitely the new XML. While XML has its strong points, JSON is preferred by most people I meet for simple messaging. JSON strengths include its lighter-weight nature and that it's purpose-built for messaging.


The Basics

 

As with XML, ESP expects specific formatting on input JSON messages. Specifically, it expects two "bracket" ([[) characters at the start of the message and two closing bracket (]]) characters at the end. Inside of these, each event is surrounded by "brace" ({}) characters as you would normally expect. So a simple input json message might look like this:

 

[
[{
"opcode": "i",
"key": "1",
"first": "steve",
"last": "foerster",
}, {
"opcode": "i",
"key": "2",
"first": "david",
"last": "robinson",
}]
]

 

In SAS ESP, the opcode metadata field is not required and will be defaulted to 'I' (Insert) if omitted. Additionally a unique key can be generated by the source window with the window option, autogen-key="true". So that is not required either. The connector/adapter message type (fstype, rmqtype, etc.) must also be set to 'JSON.' Finally, the source window schema must match the incoming fields by name. So the schema for this message would look like this in ESP Studio

 

schema.png
Nesting


Like XML, JSON supports information nesting. For example, a customer might have multiple phone numbers, multiple addresses, and/or email addresses and both XML and JSON support this by nesting. Nesting some pets into the above JSON message (and removing the opcode and id fields to emphasize that we don't need them) would look something like this:

 

[
[{
"first": "steve",
"last": "foerster",
"pets": [{
"petID": "1",
"name": "sparky"
}, {
"petID": "2",
"name": "spot"
}]
}, {
"first": "david",
"last": "robinson",
"pets": [{
"petID": "1",
"name": "mittens"
}, {
"petID": "2",
"name": "fluffy"
}]
}]
]

 

SAS ESP can resolve JSON nesting automatically in the source window. To enable this functionality, simply add field names that include the nested field names separated by underscores (_). To capture the nested pet information from the above example, you would name the source window fields, pets_petId and pets_name. To understand how the source window renders the nested data, see the image below.

 

subscription.png

 


The model

 

The ESP model used in this example is extremely simple and shows just how easy it can be to read JSON messages with SAS ESP.

 

<project name="proj" pubsub="none" threads="1">
  <contqueries>
    <contquery name="cq">
      <windows>
        <window-source name="src" insert-only="true" collapse-updates="true" autogen-key="true">
          <schema>
            <fields>
              <field name="key" type="int64" key="true"/>
              <field name="first" type="string"/>
              <field name="last" type="string"/>
              <field name="pets_petID" type="string"/>
              <field name="pets_name" type="string"/>
            </fields>
          </schema>
          <connectors>
            <connector name="conn" class="fs">
              <properties>
                <property name="type"><![CDATA[pub]]></property>
                <property name="fsname"><![CDATA[test.json]]></property>
                <property name="fstype"><![CDATA[json]]></property>
              </properties>
            </connector>
          </connectors>
        </window-source>
      </windows>
    </contquery>
  </contqueries>
</project>

 

For more information on SAS Event Stream Processing, see the software page linked below: https://www.sas.com/en_us/software/data-management/event-stream-processing.html

Version history
Last update:
‎02-16-2018 05:23 PM
Updated by:

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags