piyush-balwani piyush-balwani - 4 years ago 171
JSON Question

parse multiline tab separated Json in python

{JSON-1} {JSON-2}

{JSON-1} {JSON-2}

{JSON-1} {JSON-2}


Json is multilines and tab separated also as above. Only JSON-2 is of use.
How to parse this type of json in python?

Answer Source

Start out by ignoring the fact that the data is JSON at all.

  • Open the file
  • Iterate each line in the file using for line in f
  • Split the two tab-separated fields using line.split('\t')
  • Then convert the portion of interest into a Python object using json.loads()

Example:

import json

with open('weird_file.txt', 'r') as f:
    for line in f:
        json1, json2 = line.split('\t')
        j2 = json.loads(json2)

        # Use j2
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download