0

I have an app that I want to parse using JSON, it outputs data in the following format for each frame of a video...

root@nvidia-desktop:/home/nvidia# cat test.json 
{"version":2,"data_type":"alpr_results","epoch_time":1567039688133,"img_width":1920,"img_height":1080,"processing_time_ms":133.36436462402344,"uuid":"","error":false,"regions_of_interest":[{"x":0,"y":0,"width":1920,"height":1080}],"results":[{"plate":"LRK290","confidence":77.8797607421875,"matches_template":0,"plate_index":0,"region":"nz-auk","region_confidence":57,"processing_time_ms":73.737274169921875,"requested_topn":10,"coordinates":[{"x":667,"y":435},{"x":718,"y":433},{"x":718,"y":447},{"x":668,"y":449}],"vehicle_region":{"x":614,"y":336,"width":158,"height":158},"candidates":[{"plate":"LRK290","confidence":77.8797607421875,"matches_template":0},{"plate":"CRK290","confidence":71.4766616821289,"matches_template":0},{"plate":"RK290","confidence":69.619705200195312,"matches_template":0},{"plate":"ERK290","confidence":67.774589538574219,"matches_template":0},{"plate":"NRK290","confidence":67.747268676757812,"matches_template":0},{"plate":"QRK290","confidence":67.733123779296875,"matches_template":0},{"plate":"MRK290","confidence":67.732582092285156,"matches_template":0},{"plate":"ARK290","confidence":67.731903076171875,"matches_template":0},{"plate":"URK290","confidence":67.73187255859375,"matches_template":0},{"plate":"HRK290","confidence":67.7306137084961,"matches_template":0}]}]}
{"version":2,"data_type":"alpr_results","epoch_time":1567039688133,"img_width":1920,"img_height":1080,"processing_time_ms":133.36436462402344,"uuid":"","error":false,"regions_of_interest":[{"x":0,"y":0,"width":1920,"height":1080}],"results":[{"plate":"LRK298","confidence":73.848838806152344,"matches_template":0,"plate_index":0,"region":"nz-auk","region_confidence":60,"processing_time_ms":73.737274169921875,"requested_topn":10,"coordinates":[{"x":674,"y":439},{"x":724,"y":440},{"x":725,"y":457},{"x":675,"y":457}],"vehicle_region":{"x":600,"y":315,"width":199,"height":199},"candidates":[{"plate":"LRK298","confidence":73.848838806152344,"matches_template":0},{"plate":"LRK290","confidence":70.1683120727539,"matches_template":0},{"plate":"LRK29","confidence":65.916587829589844,"matches_template":0},{"plate":"LRK292","confidence":65.307365417480469,"matches_template":0},{"plate":"LRK293","confidence":65.293647766113281,"matches_template":0},{"plate":"LRK299","confidence":65.292098999023438,"matches_template":0},{"plate":"LRK29Q","confidence":65.289726257324219,"matches_template":0},{"plate":"LR298","confidence":61.362815856933594,"matches_template":0},{"plate":"RK298","confidence":60.530429840087891,"matches_template":0},{"plate":"LK298","confidence":60.528419494628906,"matches_template":0}]}]}
{"version":2,"data_type":"alpr_results","epoch_time":1567039688133,"img_width":1920,"img_height":1080,"processing_time_ms":133.36436462402344,"uuid":"","error":false,"regions_of_interest":[{"x":0,"y":0,"width":1920,"height":1080}],"results":[{"plate":"LRK298","confidence":82.452705383300781,"matches_template":0,"plate_index":0,"region":"nz-can","region_confidence":34,"processing_time_ms":73.737274169921875,"requested_topn":10,"coordinates":[{"x":687,"y":444},{"x":735,"y":443},{"x":737,"y":462},{"x":688,"y":463}],"vehicle_region":{"x":604,"y":310,"width":215,"height":215},"candidates":[{"plate":"LRK298","confidence":82.452705383300781,"matches_template":0},{"plate":"LRK290","confidence":73.113128662109375,"matches_template":0},{"plate":"LRK29","confidence":70.9032211303711,"matches_template":0},{"plate":"LRK292","confidence":70.755508422851562,"matches_template":0},{"plate":"LRK293","confidence":70.6309814453125,"matches_template":0},{"plate":"LRK299","confidence":70.598648071289062,"matches_template":0},{"plate":"RK298","confidence":69.2212905883789,"matches_template":0},{"plate":"LR298","confidence":68.384689331054688,"matches_template":0},{"plate":"CRK298","confidence":68.189926147460938,"matches_template":0},{"plate":"NRK298","confidence":68.101593017578125,"matches_template":0}]}]}

Here is is working with a single entry in test.json

root@nvidia-desktop:/home/nvidia# cat test.json | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["results"][0]["plate"]'
LRK290
root@nvidia-desktop:/home/nvidia# 

But it fails with multiple entries (which is what I'm wanting)

root@nvidia-desktop:/home/nvidia# cat test.json | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["results"][0]["plate"]'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/json/__init__.py", line 291, in load
    **kw)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 5 column 1 (char 1306 - 3924)
root@nvidia-desktop:/home/nvidia# 

1 Answer 1

3

Try reading stdin one line at a time, e.g.:

$ python -c 'import json,sys
for line in sys.stdin:
  obj=json.loads(line)
  print obj["results"][0]["plate"]' < test.json 
LRK290
LRK298
LRK298

If python isn't a hard requirement, jq works too:

$ jq -r .results[0].plate test.json  
LRK290
LRK298
LRK298

The -r option gives raw output. Without -r, you'll get JSON encoded output (double-quoted strings in this case).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.