ファイルの例
例えば、"ruleId"
をキーにして値を取得したいとき
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ "timestamp": 1604751455679, "ruleGroupList": [ { "ruleGroupId": "AWS#AWSManagedRulesCommonRuleSet", "terminatingRule": { "ruleId": "NoUserAgent_HEADER", "action": "BLOCK", "ruleMatchDetails": null }, "nonTerminatingMatchingRules": [], "excludedRules": null } ] } |
取り出し方
jsn["ruleGroupList"][0]["terminatingRule"]["ruleId"]
のように取り出す
1 2 3 4 5 6 |
<span class="kn">import</span> <span class="nn">json</span> <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">'test.json'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span> <span class="n">jsn</span> <span class="o">=</span> <span class="n">json</span><span class="p">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="n">jsn</span><span class="p">[</span><span class="s">"ruleGroupList"</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s">"terminatingRule"</span><span class="p">][</span><span class="s">"ruleId"</span><span class="p">])</span> |
実行結果
1 2 |
yuta:~ $ python jsontest.py NoUserAgent_HEADER |