LoginSignup
2
2

More than 5 years have passed since last update.

PythonワンライナーでJSONの要素を出力する

Last updated at Posted at 2017-10-18

こういうJSONスキーマがtest.jsonというファイルにストアされていると仮定する。

{
    "title": "Person",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        }
    }
}

titleというKeyの要素を取得する場合:

$ python -c "import json; print(json.load(open('test.json','r')).get('title'))"
Person

propertiesというKeyの要素を取得する場合:

$ python -c "import json; print(json.load(open('test.json','r')).get('properties'))"
{u'lastName': {u'type': u'string'}, u'firstName': {u'type': u'string'}}

なお、ここでは Python 2.7 における実行結果を記した。

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2