40

I am very new to Json files. If I have a json file with multiple json objects such as following:

{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",
 "Code":[{"event1":"A","result":"1"},…]}
{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",
 "Code":[{"event1":"B","result":"1"},…]}
{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",
 "Code":[{"event1":"B","result":"0"},…]}
…

I want to extract all "Timestamp" and "Usefulness" into a data frames:

    Timestamp    Usefulness
 0   20140101      Yes
 1   20140102      No
 2   20140103      No
 …

Does anyone know a general way to deal with such problems?

martineau
  • 99,260
  • 22
  • 139
  • 249
user6396
  • 1,492
  • 5
  • 18
  • 33
  • 1
    having a single json array containing all your json object would be quite easier – njzk2 Jan 12 '15 at 17:33
  • [https://stackoverflow.com/questions/53788395/tweets-streamed-using-tweepy-reading-json-file-in-python/53789187#53789187](https://stackoverflow.com/questions/53788395/tweets-streamed-using-tweepy-reading-json-file-in-python/53789187#53789187) – Diego Marino Dec 15 '18 at 04:05

4 Answers4

88

Update: I wrote a solution that doesn't require reading the entire file in one go. It's too big for a stackoverflow answer, but can be found here jsonstream.

You can use json.JSONDecoder.raw_decode to decode arbitarily big strings of "stacked" JSON (so long as they can fit in memory). raw_decode stops once it has a valid object and returns the last position where wasn't part of the parsed object. It's not documented, but you can pass this position back to raw_decode and it start parsing again from that position. Unfortunately, the Python json module doesn't accept strings that have prefixing whitespace. So we need to search to find the first none-whitespace part of your document.

from json import JSONDecoder, JSONDecodeError
import re

NOT_WHITESPACE = re.compile(r'[^\s]')

def decode_stacked(document, pos=0, decoder=JSONDecoder()):
    while True:
        match = NOT_WHITESPACE.search(document, pos)
        if not match:
            return
        pos = match.start()
        
        try:
            obj, pos = decoder.raw_decode(document, pos)
        except JSONDecodeError:
            # do something sensible if there's some error
            raise
        yield obj

s = """

{"a": 1}  


   [
1
,   
2
]


"""

for obj in decode_stacked(s):
    print(obj)

prints:

{'a': 1}
[1, 2]
Dunes
  • 32,114
  • 7
  • 68
  • 83
  • I, too, like this answer quite a bit except for a couple of things: It requires reading the entire file into memory and its use of undocumented features of the `JSONDecoder`. – martineau Feb 18 '19 at 17:30
  • 1
    Just FYI: there is a simple escape for non-whitespace characters: `\S`. The upper case variants are the negation of the lower case ones (so `\W = [^\w]`, `\D=[^\d]` ecc.) – Giacomo Alzetta Oct 21 '19 at 14:04
  • This works for AWS Lambda if the file has single line multi JSON file.. Can you explain in more details how this works? I m not able to understand raw_decode or how it can understand when a valid json starts or ends – Abilash Amarasekaran Mar 17 '21 at 00:12
29

Use a json array, in the format:

[
{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",
  "Code":[{"event1":"A","result":"1"},…]},
{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",
  "Code":[{"event1":"B","result":"1"},…]},
{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",
  "Code":[{"event1":"B","result":"0"},…]},
...
]

Then import it into your python code

import json

with open('file.json') as json_file:

    data = json.load(json_file)

Now the content of data is an array with dictionaries representing each of the elements.

You can access it easily, i.e:

data[0]["ID"]
mgrollins
  • 583
  • 2
  • 8
dfranca
  • 4,381
  • 2
  • 23
  • 49
  • 31
    This is cool, but prevents you to use the file as an endless stream (e.g. log-like append-only file data) and consumes a lot more memory. – exa Dec 01 '15 at 13:24
  • 1
    @exa, this is true, but if you need append-only logging for this data stream, perhaps you should be looking at a format other than JSON to transfer your information, as JSON requires the closing bracket for all data structures, implying a non-infinite non-stream format. – David Culbreth Jul 17 '19 at 19:49
11

So, as was mentioned in a couple comments containing the data in an array is simpler but the solution does not scale well in terms of efficiency as the data set size increases. You really should only use an iterator when you want to access a random object in the array, otherwise, generators are the way to go. Below I have prototyped a reader function which reads each json object individually and returns a generator.

The basic idea is to signal the reader to split on the carriage character "\n" (or "\r\n" for Windows). Python can do this with the file.readline() function.

import json
def json_reader(filename):
    with open(filename) as f:
        for line in f:
            yield json.loads(line)

However, this method only really works when the file is written as you have it -- with each object separated by a newline character. Below I wrote an example of a writer that separates an array of json objects and saves each one on a new line.

def json_writer(file, json_objects):
    with open(file, "w") as f:
        for jsonobj in json_objects:
            jsonstr = json.dumps(jsonobj)
            f.write(jsonstr + "\n")

You could also do the same operation with file.writelines() and a list comprehension:

...
    json_strs = [json.dumps(j) + "\n" for j in json_objects]
    f.writelines(json_strs)
...

And if you wanted to append the data instead of writing a new file just change open(file, "w") to open(file, "a").

In the end I find this helps a great deal not only with readability when I try and open json files in a text editor but also in terms of using memory more efficiently.

On that note if you change your mind at some point and you want a list out of the reader, Python allows you to put a generator function inside of a list and populate the list automatically. In other words, just write

lst = list(json_reader(file))
Boris
  • 7,044
  • 6
  • 62
  • 63
Dan Temkin
  • 1,247
  • 1
  • 9
  • 16
4

Added streaming support based on the answer of @dunes:

import re
from json import JSONDecoder, JSONDecodeError

NOT_WHITESPACE = re.compile(r"[^\s]")


def stream_json(file_obj, buf_size=1024, decoder=JSONDecoder()):
    buf = ""
    ex = None
    while True:
        block = file_obj.read(buf_size)
        if not block:
            break
        buf += block
        pos = 0
        while True:
            match = NOT_WHITESPACE.search(buf, pos)
            if not match:
                break
            pos = match.start()
            try:
                obj, pos = decoder.raw_decode(buf, pos)
            except JSONDecodeError as e:
                ex = e
                break
            else:
                ex = None
                yield obj
        buf = buf[pos:]
    if ex is not None:
        raise ex
Fantix King
  • 1,216
  • 1
  • 12
  • 13