From da187b22f1cccfe1a2176197996818da9763a402 Mon Sep 17 00:00:00 2001 From: Morteza Hosseini Date: Fri, 5 Apr 2024 15:49:00 +0100 Subject: [PATCH] Add FAQs --- terminal/jq.ipynb | 109 ++++++++++++++++++++++++++++++++- website/docs/terminal/jq.md | 118 ++++++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+), 1 deletion(-) diff --git a/terminal/jq.ipynb b/terminal/jq.ipynb index 835409a1..da359c1c 100644 --- a/terminal/jq.ipynb +++ b/terminal/jq.ipynb @@ -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": { @@ -512,7 +619,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" + "version": "3.12.2" } }, "nbformat": 4, diff --git a/website/docs/terminal/jq.md b/website/docs/terminal/jq.md index 4da5369f..bf05a55a 100644 --- a/website/docs/terminal/jq.md +++ b/website/docs/terminal/jq.md @@ -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