21

I have huge json objects containing 2D lists of coordinates that I need to transform into numpy arrays for processing.

However using json.loads followed with np.array() is too slow.

Is there a way to increase the speed of creation of numpy arrays from json?

import json
import numpy as np

json_input = '{"rings" : [[[-8081441.0, 5685214.0], [-8081446.0, 5685216.0], [-8081442.0, 5685219.0], [-8081440.0, 5685211.0], [-8081441.0, 5685214.0]]]}'

dict = json.loads(json_input)
numpy_2d_arrays = [np.array(ring) for ring in dict["rings"]]

I would take any solution whatsoever!

4
  • I'm getting a json decoder error: ` File "/usr/lib/python3.5,... json.decoder.JSONDecodeError: Expecting value: line 1 column 14 (char 13)` Commented Dec 9, 2016 at 23:15
  • @hpaulj you are right, it's fixed Commented Dec 10, 2016 at 0:29
  • It was the () which were non-standard JSON. Commented Dec 10, 2016 at 0:46
  • Other parsers are eval and ast.literal_eval (safer). On this small sample json.loads is noticeably faster. The np.array part takes even less time. Commented Dec 10, 2016 at 1:32

3 Answers 3

6

The simplest answer would just be:

numpy_2d_arrays = np.array(dict["rings"])

As this avoids explicitly looping over your array in python you would probably see a modest speedup. If you have control over the creation of json_input it would be better to write out as a serial array. A version is here.

Sign up to request clarification or add additional context in comments.

Comments

4

Since JSON syntax is really near to Python syntax, I suggest you to use ast.literal_eval. It may be faster…

import ast
import numpy as np

json_input = """{"rings" : [[[-8081441.0, 5685214.0],
                             [-8081446.0, 5685216.0],
                             [-8081442.0, 5685219.0],
                             [-8081440.0, 5685211.0],
                             [-8081441.0, 5685214.0]]]}"""

rings = ast.literal_eval(json_input)
numpy_2d_arrays = [np.array(ring) for ring in rings["rings"]]

Give it a try. And tell us.

1 Comment

Thank you but ast.literal_eval is slower than json.loads() with my data
3

For this specific data, you could try this

import numpy as np

json_input = '{"rings" : [[(-8081441.0, 5685214.0), (-8081446.0, 5685216.0), (-8081442.0, 5685219.0), (-8081440.0, 5685211.0), (-8081441.0, 5685214.0)]]}'
i = json_input.find('[')
L = eval(json_input[i+1:-2])
print(np.array(L))

2 Comments

using eval is unsafe
@ei-grad It depends on the context. Python has always been made for consenting adults!

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.