Decoding for JSON
This section describes how to decode a JSON
NotificationContentList
.
A
NotificationContentList
can include multiple values. If a
NotificationContentList
includes multiple values, each value must be
concatenated into one string when decoding. The following is an example of a
NotificationContentList
in JSON API format.
"InitiatorId": "0-1-5-71",
"NotificationContentFlags": 1,
"NotificationContentList": ["H4sIAAAAAAAAAO1aW3PaOBR+z69gPPu2ddfmkgtvBEjKbAJsIJ3Z3ekwwhbErbFZ2U7KdPLfV5It\r\nX2TLsmFD2m6YPAR0ztGRzqdPR9L5dtLAH+UXpdtQbv2vl4Fj2nAaIOMBeHDs+tbKMoBvuY7yLpS8\r\nBDZwDDjabIHhXyM32N5Yno/V/6bt5PMt/i9rO605clYu1Y7sxtIZqZzp4i7kXXG98L31beB5IxNr\r\n65qmVZAdgw0kfbVmQ3XQm/fUcW+uDu/VnudBX9LZ0DHnVqje1PQLVWuq+ulc17sdvau33mv086um\r\ndzVNYikc32S1gkg4U+UzVj5z1HLJ9BX7c781gQ+lDlVzrNzBsKsKHsZmehs3cIhjqq61zztnp9p7\r\nQbxzqmFn892WhK4l1XkulfgkmdE+8IHtrkc+3Ay/+hA5wKb4pIi7vuslqGtpA1l4Usaokfa5ROHK\r\nBmsPCzYlchQgef8uBzfq+G7Yr+1oCLhKLlLRO+i5mKsgVZHEMdT4CJFH
To decode a JSON
NotificationContentList
, create a Python script named
decode.py with the following code:
import sys
import json
import base64
import gzip
import shutil
#from pprint import pprint
def decodeJson(jsonFileName):
global counter
with open(jsonFileName) as f:
data = json.load(f)
# get list of events
events = data.get('EventList')
if events:
for event in events:
# get notification list
eventDetails = event.get("eventDetails")
notificationContentList = eventDetails.get("NotificationContentList")
if notificationContentList:
counter += 1
# concat the base64/gzip compressed string
encoded = ""
for notification in notificationContentList:
encoded += notification
# base64 decode the concat string
compressedData = base64.b64decode(encoded)
txtFileName = "out" + str(counter) + ".txt"
gzFileName = txtFileName + ".gz"
print("writing gz file " + gzFileName)
# write out the gz file
with open(gzFileName, 'wb') as outfile:
outfile.write(compressedData)
# gzip decompresed
with gzip.open(gzFileName, 'rb') as f_in:
with open(txtFileName, 'wb') as f_out:
print("out=" + txtFileName)
shutil.copyfileobj(f_in, f_out)
counter = 0
for arg in sys.argv[1:]:
print("running with " + str(arg))
decodeJson(arg)
To decode, run the following command:
python3 decode.py file.json
The
file.json is a file that contains the
NotificationContentList
.
The decode.py script creates one or more files with the decoded contents of
the NotificationContentList
.