3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pythonでJSONデータの2階層目移行のキーを取得する

Posted at

ファイルの例

例えば、"ruleId"をキーにして値を取得したいとき

test.json
{
    "timestamp": 1604751455679,                                                                   
    "ruleGroupList": [
        {
            "ruleGroupId": "AWS#AWSManagedRulesCommonRuleSet",
            "terminatingRule": {
                "ruleId": "NoUserAgent_HEADER",
                "action": "BLOCK",
                "ruleMatchDetails": null
            },
            "nonTerminatingMatchingRules": [], 
            "excludedRules": null
        }
    ]   
}

取り出し方

  • jsn["ruleGroupList"][0]["terminatingRule"]["ruleId"]のように取り出す
import json

with open('test.json') as f:
    jsn = json.load(f)

print(jsn["ruleGroupList"][0]["terminatingRule"]["ruleId"])

実行結果

yuta:~ $ python jsontest.py 
NoUserAgent_HEADER
3
1
0

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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?