title: Parse valid JSON object or array from a string
body: <p>I have a string that can be one of two forms:</p>
  
  <pre><code>name multi word description {...}
  </code></pre>
  
  <p>or</p>
  
  <pre><code>name multi word description [...]
  </code></pre>
  
  <p>where <code>{...}</code> and <code>[...]</code> are any valid JSON. I am interested in parsing out just the JSON part of the string, but I'm not sure of the best way to do it (especially since I don't know which of the two forms the string will be). This is my current method:</p>
  
  <pre><code>import json
  
  string = 'bob1: The ceo of the company {"salary": 100000}' 
  o_ind = string.find('{')
  a_ind = string.find('[')
  
  if o_ind == -1 and a_ind == -1:
      print("Could not find JSON")
      exit(0)
  
  index = min(o_ind, a_ind)
  if index == -1:
      index = max(o_ind, a_ind)
  
  json = json.loads(string[index:])
  print(json)
  </code></pre>
  
  <p>It works, but I can't help but feel like it could be done better. I thought maybe regex, but I was having trouble with it matching sub objects and arrays and not the outermost json object or array. Any suggestions?</p>