0

I'm trying to read (multiple) streams at the same time and extract only the JSON data from it.

So I have an ANPR camera that allows me to continiously "listen" to it and will send data on an URL with an image and json payload in it with the detected plate.

HTTP/1.1 200 OK
Pragma: no-cache
Expires: Thu, 01 Dec 2003 16:00:00 GMT
Connection: close
Content-Type: multipart/mixed; boundary=IPCamEventStreamBoundary
Cache: no-cache
Accept-Ranges: none
X-KeepAlive: 0
X-Timestamp: 1620986981098
X-Windows-Timestamp: 13265460581098
--IPCamEventStreamBoundary
Content-Type: image/jpeg
Content-Length: 498749
X-Timestamp: 1620986982002
X-Image-Index: 1
X-Frame-Id: 521699
X-Frame-Timestamp: 757030579
X-Frame-Width: 2560
X-Frame-Height: 1920
**binary data of the image goes here**
--IPCamEventStreamBoundary
Content-Type: application/json
Content-Length: 308
X-Event-Index: 1
X-Timestamp: 1620986982042
{
    "DetectorVersion" : 131072,
    "DetectorID" : "{6309907F-5708-47D1-B410-50F02C8882FB}",
    "DetectorClassID" : -835316578,
    "EventTime" : "13265460582042",
    "State" : "dsSignal",
    "EventCode" : 100,
    "EventInfo" : {},
    "EventID" : "{4F34A399-9E02-1846-ADA7-98A2798B46B9}",
    "DetectorEventType" : "detSignal"
}

I only need the application/json part.

What I currently have, is the whole readout of everything, but I can't seem to just "decode" the json and use that data.

This is my current code:

import json
import requests

r = requests.get('http://ip:port/stream', stream=True)

if r.encoding is None:
    r.encoding = 'utf-8'

for line in r.iter_lines(decode_unicode=True):
    if line:
        try:
            # print(json.loads(line))
            print(line)
        except:
            pass

How am I supposed to do this? Just skipping the first binary content and only read the JSON?

Robin
  • 1,567
  • 3
  • 25
  • 67

1 Answers1

0

I've used Martijn Pieters' solution for detecting JSON objects in strings from here

However, the only way I could test this was using a text file with the output that you post.

import regex
import json
import requests

r = requests.get('http://ip:port/stream', stream=True)

if r.encoding is None:
    r.encoding = 'utf-8'

text = ''
for line in r.iter_lines(decode_unicode=True):
    text += str(line)

pattern = regex.compile(r'\{(?:[^{}]|(?R))*\}')
json_string_list = pattern.findall(text)
print([json.loads(json_string) for json_string in json_string_list])
Ze'ev Ben-Tsvi
  • 1,174
  • 1
  • 3
  • 7
  • Thanks for the comment! I tried this, but I don't see any response in my console :( – Robin Apr 27 '22 at 11:30
  • I did log in the for loop the line, this prints all the contents, but for some reason, they don't get appended to the text – Robin Apr 27 '22 at 11:39