Skip to content

Commit

Permalink
Add FAQs
Browse files Browse the repository at this point in the history
  • Loading branch information
smortezah committed Apr 5, 2024
1 parent 3632ffd commit da187b2
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 1 deletion.
109 changes: 108 additions & 1 deletion terminal/jq.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,113 @@
"source": [
"!echo '{\"name\": \"Alice\", \"age\": 30}' | jq '.city'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## FAQs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q:** How can I extract specific elements from a deeply nested JSON structure using jq?"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;39m{\n",
" \u001b[0m\u001b[1;34m\"city\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"New York\"\u001b[0m\u001b[1;39m,\n",
" \u001b[0m\u001b[1;34m\"street\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"123 Main St\"\u001b[0m\u001b[1;39m\n",
"\u001b[1;39m}\u001b[0m\n"
]
}
],
"source": [
"!echo '{\"person\": [{\"name\": \"Alice\", \"address\": {\"city\": \"New York\", \"street\": \"123 Main St\"}}, {\"name\": \"Bob\", \"address\": {\"city\": \"Los Angeles\", \"street\": \"456 Elm St\"}}]}' | jq '.person[].address | select(.city == \"New York\")'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q:** How do I modify and update values within a JSON object using jq?"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"!echo '{\"product\": {\"name\": \"Widget\", \"price\": 25.99, \"category\": \"Electronics\"}}' | jq '.product.price = 29.99' > output.json"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q:** Can jq aggregate data within JSON arrays?"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[0;39m70\u001b[0m\n"
]
}
],
"source": [
"!echo '{\"product\": [{\"name\": \"Widget\", \"price\": 20, \"category\": \"Electronics\"}, {\"name\": \"Gadget\", \"price\": 50, \"category\": \"Electronics\"}]}' | jq 'map(.[].price) | add'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q:** How can I filter and extract specific elements from JSON arrays using jq?"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;39m[\n",
" \u001b[1;39m{\n",
" \u001b[0m\u001b[1;34m\"name\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"Product B\"\u001b[0m\u001b[1;39m,\n",
" \u001b[0m\u001b[1;34m\"price\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;39m69.99\u001b[0m\u001b[1;39m\n",
" \u001b[1;39m}\u001b[0m\u001b[1;39m,\n",
" \u001b[1;39m{\n",
" \u001b[0m\u001b[1;34m\"name\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"Product C\"\u001b[0m\u001b[1;39m,\n",
" \u001b[0m\u001b[1;34m\"price\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;39m89.99\u001b[0m\u001b[1;39m\n",
" \u001b[1;39m}\u001b[0m\u001b[1;39m\n",
"\u001b[1;39m]\u001b[0m\n"
]
}
],
"source": [
"!echo '[{\"name\": \"Product A\", \"price\": 49.99}, {\"name\": \"Product B\", \"price\": 69.99}, {\"name\": \"Product C\", \"price\": 89.99}]' | jq 'map(select(.price > 50))'"
]
}
],
"metadata": {
Expand All @@ -512,7 +619,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
118 changes: 118 additions & 0 deletions website/docs/terminal/jq.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,124 @@ null

No tantrums, no crashes-just a polite `null`.

## FAQs

**Q: How can I extract specific elements from a deeply nested JSON structure using jq?**

**A:** You can efficiently handle complex JSON path queries with jq. For instance, to retrieve all addresses with the city "New York" from a nested JSON data, you can use the following command:

```json title="file.json"
{
"person": [
{
"name": "Alice",
"address": { "city": "New York", "street": "123 Main St" }
},
{
"name": "Bob",
"address": { "city": "Los Angeles", "street": "456 Elm St" }
}
]
}
```

```bash title="Shell"
cat file.json | jq '.person[].address | select(.city == "New York")'
```

```
{
"city": "New York",
"street": "123 Main St"
}
```

This command filters and extracts the relevant address elements.

**Q: How do I modify and update values within a JSON object using jq?**

**A:** Beyond querying, jq allows you to modify and update JSON data. Suppose you need to update the "price" field in a JSON object to 29.99. You can achieve this with the following command:

```json title="file.json"
{
"product": {
"name": "Widget",
"price": 25.99,
"category": "Electronics"
}
}
```

```bash title="Shell"
cat file.json | jq '.product.price = 29.99' > output.json
```

```json title="output.json"
{
"product": {
"name": "Widget",
"price": 29.99,
"category": "Electronics"
}
}
```

This command updates the price value and saves it as "output.json".

**Q: Can jq aggregate data within JSON arrays?**

**A:** Absolutely! jq excels at aggregating and summarizing data within JSON arrays. To calculate the total price of products in an array, you can use:

```json title="file.json"
{
"product": [
{ "name": "Widget", "price": 20, "category": "Electronics" },
{ "name": "Gadget", "price": 50, "category": "Electronics" }
]
}
```

```bash title="Shell"
cat file.json | jq 'map(.[].price) | add'
```

```
70
```

This command sums up the prices of all products in the JSON array.

**Q: How can I filter and extract specific elements from JSON arrays using jq?**

**A:** Suppose you want to filter products with prices above $50. jq can efficiently achieve this:

```json title="file.json"
[
{ "name": "Product A", "price": 49.99 },
{ "name": "Product B", "price": 69.99 },
{ "name": "Product C", "price": 89.99 }
]
```

```bash title="Shell"
cat file.json | jq 'map(select(.price > 50))'
```

```
[
{
"name": "Product B",
"price": 69.99
},
{
"name": "Product C",
"price": 89.99
}
]
```

This command filters the JSON array to include only products with prices above $50.

## Putting It All Together

### Real-world use cases
Expand Down

0 comments on commit da187b2

Please sign in to comment.