-1

I have this string:

{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
"low":"265.6", "prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
"bidexch":"Q","biddate":"1513293904000","ask":265.42,
"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09",
"high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}

If I do: type(string)

I get:

<type 'unicode'> 

If I do:

type(firstString)

where firstString is just the first of the three parts of the string, I get:

<type 'unicode'>

With Python, how can I split it based on the external parentheses, such that from this one string, we obtain three strings, each one having the form "{ ... }"?

Everett
  • 387
  • 1
  • 4
  • 17
  • 2
    That's not a string... should it all be inside quotes(it should if it's a string)? You simply have 3 dicts written here... just saying. – Totem Dec 15 '17 at 00:24
  • Can those strings (I guess JSON concentrates) have more levels (i.e. contain other _objects_)? – zwer Dec 15 '17 at 00:25
  • It is not inside quotes. type(string) returns . None of these strings contain other objects. – Everett Dec 15 '17 at 00:28
  • Possible duplicate of [Parse multiple json objects that are in one line](https://stackoverflow.com/questions/36967236/parse-multiple-json-objects-that-are-in-one-line) – John Szakmeister Dec 15 '17 at 00:45

2 Answers2

0

You can split the string on the newline character and parse it using the json module.

import json

s = '''{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,"bidexch":"Q","biddate":"1513293904000","ask":265.42,"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}'''

[json.loads(line) for line in s.split('\n')]

# returns:
[{'close': '265.66',
  'high': '267.22',
  'low': '265.6',
  'open': '267.09',
  'prevClose': '266.75',
  'symbol': 'SPY',
  'type': 'summary'},
 {'ask': 265.42,
  'askdate': '1513294015000',
  'askexch': 'P',
  'asksz': 45,
  'bid': 265.38,
  'biddate': '1513293904000',
  'bidexch': 'Q',
  'bidsz': 3,
  'symbol': 'SPY',
  'type': 'quote'},
 {'close': '265.66',
  'high': '267.22',
  'low': '265.6',
  'open': '267.09',
  'prevClose': '266.75',
  'symbol': 'SPY',
  'type': 'summary'}]
James
  • 32,991
  • 4
  • 47
  • 70
  • The string itself does not have a newline character. It is one big chunk. – Everett Dec 15 '17 at 00:30
  • Can you modify your post to show the exact form of the string? In what you currently have posted, there are newlines between the curly brackets. – James Dec 15 '17 at 00:31
0

If there are no nested objects and newlines are not guaranteed between parts, as you've said, it's as simple as splitting on }:

your_string = '''{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
"low":"265.6", "prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
"bidexch":"Q","biddate":"1513293904000","ask":265.42,
"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09",
"high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}'''

substrings = [part.strip() + "}" for part in your_string.split("}") if part.strip()]
# ['{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
#  "low":"265.6", "prevClose":"266.75","close":"265.66"}',
#  '{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
#  "bidexch":"Q","biddate":"1513293904000","ask":265.42,
#  "asksz":45,"askexch":"P","askdate":"1513294015000"}',
#  '{"type":"summary","symbol":"SPY","open":"267.09",
#  "high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}']

Or you can immediately parse the parts into individual Python dictionaries:

dicts = [json.loads(part + "}") for part in your_string.split("}") if part.strip()]
# [{'open': '267.09', 'high': '267.22', 'prevClose': '266.75', 'type': 'summary',
#   'close': '265.66', 'low': '265.6', 'symbol': 'SPY'},
#  {'askdate': '1513294015000', 'bid': 265.38, 'asksz': 45, 'type': 'quote',
#   'ask': 265.42, 'bidsz': 3, 'bidexch': 'Q', 'biddate': '1513293904000',
#   'askexch': 'P', 'symbol': 'SPY'},
#  {'open': '267.09', 'high': '267.22', 'prevClose': '266.75', 'type': 'summary',
#   'close': '265.66', 'low': '265.6', 'symbol': 'SPY'}]
zwer
  • 24,943
  • 3
  • 48
  • 66