Investigating a JSON
Yesterday’s task was opening a very large JSON data file.
JSON is a serialized text format. It has only one line so we can’t display the first or last lines with head
or tail
.
But, it’s still text, so we You can give it a quick look with cat
cat FILE
and just stare at words on the screen. Not very useful, let’s see the first 1000 characters
grep -o '^.\{1000\}' FILE
We can see the beginning part but it really isn’t saying much. What we want to see is the structure of the file, the keys of the key-value pairs.
We can do that with python
import json
jsf="" # the json file name
with open (jsf) as f:
data=json.load(f)
print(data.keys)
For the nested key-value pairs, we can look inside the same way.
For example, suppose we have a property called “summary” whose value is {"key1":value1,"key2":value2,...}
within this structure. This is how to display the keys:
import json
jsf="" # the json file name
with open (jsf) as f:
data=json.load(f)
print(data['summary'].keys)