0

Lets say I have an incoming string oversocket and it look like this

'text.....text {"foo": {"bar":100}} text {"bar":2} test {"foo"'

What is the best way/library to extract only the json objects out of the incoming string?

I have tried simplejson.JSONDecoder from simple json library. However, it is not only finding objects or I didn't know how to use it.

I have tried something like this so far

import simplejson as json
input_buffer = ""
def in_data(data):
    input_buffer += data
    try:
        dict, idx = json.JSONDecoder().raw_decode(input_buffer)
    except:
        #handle exception in case nothing found 
    self.handle_input(dict) #send the dictionary for processing
    input_buffer = input_buffer[idx:]
1
  • 1
    what you have tried so far ? show some code. Commented Mar 5, 2020 at 11:47

1 Answer 1

1

Pure python solution based on simple bracket counting and trying to parse gathered text.

import json

inp = list(r'text {"foo": {"bar":100}} text {"bar":2} test ')
stack = ''
bracket_counter = 0
jsons = []

while inp:
    char = inp.pop(0)
    if char == '{':
        bracket_counter += 1

    if bracket_counter:
        stack += char

    if char == '}':
        bracket_counter -= 1
        if bracket_counter == 0:
            try:
                parsed = json.loads(stack)
                jsons.append(parsed)
            except json.JSONDecodeError:
                pass
            stack = ''

print(jsons)  # -> [{'foo': {'bar': 100}}, {'bar': 2}]
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.