From 7f8975412524556aeed91f9a002c4d32b908ac07 Mon Sep 17 00:00:00 2001 From: MichiRecRoom <1008889+LikeLakers2@users.noreply.github.com> Date: Fri, 27 Feb 2026 15:34:17 -0500 Subject: [PATCH] Flatten the JSON files in `/cookbook/http.md` --- cookbook/http.md | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/cookbook/http.md b/cookbook/http.md index fbdf144a687..b51b22d1559 100644 --- a/cookbook/http.md +++ b/cookbook/http.md @@ -47,17 +47,15 @@ perhaps with different query parameters and you want to view all the responses a An example JSON file, `urls.json`, with the following contents: ```json -{ - "urls": [ - "https://jsonplaceholder.typicode.com/posts/1", - "https://jsonplaceholder.typicode.com/posts/2", - "https://jsonplaceholder.typicode.com/posts/3" - ] -} +[ + "https://jsonplaceholder.typicode.com/posts/1", + "https://jsonplaceholder.typicode.com/posts/2", + "https://jsonplaceholder.typicode.com/posts/3" +] ``` ```nu -open urls.json | get urls | each { |u| http get $u } +open urls.json | each { |u| http get $u } # => ━━━┯━━━━━━━━┯━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # => # │ userId │ id │ title │ body # => ───┼────────┼────┼─────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────── @@ -83,7 +81,7 @@ open urls.json | get urls | each { |u| http get $u } If you specify the `--raw` flag, you'll see 3 separate json objects, one in each row. ```nu -open urls.json | get urls | each { |u| http get $u -r } +open urls.json | each { |u| http get $u -r } # => ━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # => # │ # => ───┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── @@ -116,7 +114,7 @@ open urls.json | get urls | each { |u| http get $u -r } To combine these responses together into a valid JSON array, you can turn the table into json. ```nu -open urls.json | get urls | each { |u| http get $u } | to json +open urls.json | each { |u| http get $u } | to json ``` Output @@ -150,16 +148,14 @@ Making a `post` request to an endpoint with a JSON payload. To make long request ```json { - "my_payload": { - "title": "foo", - "body": "bar", - "userId": 1 - } + "title": "foo", + "body": "bar", + "userId": 1 } ``` ```nu -open payload.json | get my_payload | to json | http post https://jsonplaceholder.typicode.com/posts $in +open payload.json | to json | http post https://jsonplaceholder.typicode.com/posts $in # => ━━━━━ # => id # => ───── @@ -172,7 +168,7 @@ open payload.json | get my_payload | to json | http post https://jsonplaceholder We can put this all together into a pipeline where we read data, manipulate it, and then send it back to the API. Lets `fetch` a post, `increment` the id, and `post` it back to the endpoint. In this particular example, the test endpoint gives back an arbitrary response which we can't actually mutate. ```nu -open urls.json | get urls | first | http get $in | upsert id {|item| $item.id | inc} | to json | http post https://jsonplaceholder.typicode.com/posts $in +open urls.json | first | http get $in | upsert id {|item| $item.id | inc} | to json | http post https://jsonplaceholder.typicode.com/posts $in # => ━━━━━ # => id # => ─────