在Linux命令行中解析JSON的详细步骤
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">前言</a></li><li><a href="#_label1">示例 JSON 文件内容</a></li><li><a href="#_label2">使用示例</a></li></ul></div><p class="maodian"><a name="_label0"></a></p><h2>前言</h2><p>JQ 是一个通用的 Linux 命令行 JSON 处理器,它允许开发人员快速地解析、过滤和转换 JSON 数据。在本文中,我们将介绍常用的 JQ 命令,展示如何简化 JSON 处理任务。</p>
<p class="maodian"><a name="_label1"></a></p><h2>示例 JSON 文件内容</h2>
<p>您可以将以下示例数据保存在名为 <strong>input.json</strong> 的文件中。这个 JSON 数据表示具有各自姓名、年龄、国家和地址的人员数组。</p>
<div class="jb51code"><pre class="brush:json;">[
{
"name": "Alice",
"age": 35,
"country": "USA",
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001"
}
},
{
"name": "Bob",
"age": 28,
"country": "Canada",
"address": {
"street": "456 Maple Ave",
"city": "Toronto",
"province": "ON",
"postal_code": "M5V 1A1"
}
},
{
"name": "Charlie",
"age": 42,
"country": "USA",
"address": {
"street": "789 Oak St",
"city": "San Francisco",
"state": "CA",
"zip": "94102"
}
},
{
"name": "David",
"age": 23,
"country": "Canada",
"address": {
"street": "321 Pine St",
"city": "Vancouver",
"province": "BC",
"postal_code": "V6B 2P4"
}
}
]
</pre></div>
<p class="maodian"><a name="_label2"></a></p><h2>使用示例</h2>
<p><strong>1. Pretty Print JSON Data</strong></p>
<p>要美观地打印 JSON 数据,只需将 JSON 文件传递给 JQ 命令,后跟一个句点(“.”):</p>
<div class="jb51code"><pre class="brush:bash;">jq '.' input.json
</pre></div>
<p><strong>2. Retrieve a Specific JSON Key</strong></p>
<p>要从 JSON 对象中提取特定键的值,使用以下语法:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.key' input.json
</pre></div>
<p>例如,要提取 <strong>name</strong> 键的值,使用:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.name' input.json
</pre></div>
<p><strong>3. Access Nested JSON Values</strong></p>
<p>要访问嵌套的 JSON 值,使用点(“.”)表示法:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.key1.key2.key3' input.json
</pre></div>
<p>例如,要提取嵌套在<strong>address</strong> 下的 <strong>city</strong> 键的值,使用:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.address.city' input.json
</pre></div>
<p><strong>4. Iterate Over JSON Arrays</strong></p>
<p>使用方括号(“[]”)来迭代 JSON 数组:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.[]' input.json
</pre></div>
<p>要从数组中的每个对象提取特定的键,使用 pipe(“|”)操作符:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.[] | .key' input.json
</pre></div>
<p><strong>5. Filter JSON Data</strong></p>
<p>如果需要根据特定条件过滤 JSON 数据,可以使用 select 函数:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.[] | select(.key == "value")' input.json
</pre></div>
<p>例如,要过滤数组中年龄大于 30 的对象,使用:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.[] | select(.age > 30)' input.json
</pre></div>
<p><strong>6. Map and Transform JSON Data</strong></p>
<p>要映射和转换 JSON 数据,使用大括号(“{}”):</p>
<div class="jb51code"><pre class="brush:bash;">jq '.[] | {key1: .key1, key2: .key2}' input.json
</pre></div>
<p>例如,要创建一个只有 <strong>name</strong> 和 <strong>age</strong> 键的 JSON 对象,使用:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.[] | {name: .name, age: .age}' input.json
</pre></div>
<p><strong>7. Combine Multiple JSON Files</strong></p>
<p>要合并两个 JSON 文件,使用 “*” 操作符:</p>
<div class="jb51code"><pre class="brush:bash;">jq -s '. * .' file1.json file2.json
</pre></div>
<p><strong>8. Perform Arithmetic Operations</strong></p>
<p>JQ 可以对数值 JSON 值执行算术运算:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.number1 + .number2' input.json
</pre></div>
<p><strong>9. Sort JSON Data</strong></p>
<p>要根据特定键对 JSON 数据进行排序,使用 <strong>sort_by</strong> 函数:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.[] | sort_by(.key)' input.json
</pre></div>
<p>| 1 | <strong>jq</strong>’.[] | sort_by(.key)'<strong>input</strong>.json |<br />| — | ------------------------------------------- |</p>
<p>例如,按照 <strong>age</strong> 键对对象数组进行排序,请使用:</p>
<div class="jb51code"><pre class="brush:bash;">jq '.[] | sort_by(.age)' input.json
</pre></div>
<p><strong>10. Group JSON Data</strong></p>
<p>要根据特定键对 JSON 数据进行分组,使用 <strong>group_by</strong> 函数:</p>
<div class="jb51code"><pre class="brush:bash;">jq 'group_by(.key)' input.json
</pre></div>
<p>例如,按照 <strong>country</strong> 键对对象数组进行分组,请使用:</p>
<div class="jb51code"><pre class="brush:bash;">jq 'group_by(.country)' input.json
</pre></div>
頁:
[1]