Parse only one json level

I have the following line:

'{ "key1": "val1", "key2": ["a","b", 3], "key3": {"foo": 27, "bar": [1, 2, 3]} }' 

I want to analyze only one level, so the result should be a single-level dictionary with a key, and the value should be just a string (no need to analyze it)

For this string, it should return the following dictionary:

 { "key1": "val1", "key2": "['a','b', 3]", "key3": "{'foo': 27, 'bar': [1, 2, 3]}" } 

Is there a quick way to do this? Without parsing the entire string in json and converting all values ​​back to strings.

+10
json python
source share
3 answers

There is hardly an answer, but I see only two possibilities:

  • Download full JSON and discard the values ​​you excluded in your question
  • Modify the content by wrapping the values ​​in quotation marks so that JSON loading returns string values

Honestly, I think there is no such thing as a “critical JSON performance analysis”, it just sounds wrong, so I would go with the first option.

+3
source share

I think you can solve this with a regex, it works for me:

 import re pattern = re.compile('"([a-zA-Z0-9]+)"\s*:\s*(".*"|\[.*\]|\{.*\})') dict(re.findall(pattern, json_string)) 

But I do not know if it is faster, you need to try to use your data.

[EDIT]

Yes, it's faster. I tried the scripts below, and the regex version is 5 times faster.

using json module:

 import json val=''' { "key1": "val1", "key2": ["a","b", 3], "key3": {"foo": 27, "bar": [1, 2, 3]} } ''' for n in range(100000): dict((k,json.dumps(v)) for k,v in json.loads(val).items()) 

using regex:

 import re val='''{ "key1": "val1", "key2": ["a","b", 3], "key3": {"foo": 27, "bar": [1, 2, 3]} }''' pattern = re.compile('"([a-zA-Z0-9]+)"\s*:\s*(".*"|\[.*\]|\{.*\})') for n in range(100000): dict(re.findall(pattern, val)) 
+3
source share

I'm not right now if this is really what you need, but try

 >>> import json >>> val = """ ... { ... "key1": "val1", ... "key2": ["a","b", 3], ... "key3": {"foo": 27, "bar": [1, 2, 3]} ... } ... """ >>> dict((k,json.dumps(v)) for k,v in json.loads(val).items()) {u'key3': '{"foo": 27, "bar": [1, 2, 3]}', u'key2': '["a", "b", 3]', u'key1': '"val1"'} 

This is a bit complicated because you are loading the complete JSON object, and not just returning the values ​​in the dictionary.

0
source share

All Articles