5

While I still prefer a more "traditional" shell for day-to-day use, I'm finding Nushell's structured data capabilities to be a fantastic companion to the SE API (among others). It's been months since I've used jq for processing JSON REST results.

Starting with a simple example:

# Get the last 100 questions posted on Stack Overflow
> let res = (http get "https://api.stackexchange.com/questions?pagesize=100&site=stackoverflow")
# Will error if you are out of quota - Append a key param if needed

> $res.items.title | first 5
╭───┬───────────────────────────────────────────────────────────────────────────────╮
│ 0 │ Same Framework for multiple target and different destinations                 │
│ 1 │ Unable to control full screen menu bar visibility on MacOS using CLI defaults │
│ 2 │ How should MqttClient lifecycle be managed?                                   │
│ 3 │ Auth0 External Provider                                                       │
│ 4 │ Passing a function to a react-router action in TypeScript?                    │
╰───┴───────────────────────────────────────────────────────────────────────────────╯

> $res.quota_remaining
167

> $res
# Will print the data (truncate based on screen width) in a table format

> $res | explore
# Will enter a data explorer view where you can scroll and drill down into data
# For example, select the "items" column and hit <Enter> to drill down to the questions themselves

# What key/value pairs (columns) are available in the result?
> $res | columns
╭───┬─────────────────╮
│ 0 │ items           │
│ 1 │ has_more        │
│ 2 │ quota_max       │
│ 3 │ quota_remaining │
╰───┴─────────────────╯

# What key/value pairs (columns) are available for items?
> $res.items | columns
╭────┬───────────────────────╮
│  0 │ tags                  │
│  1 │ owner                 │
│  2 │ post_state            │
│  3 │ is_answered           │
│  4 │ view_count            │
│  5 │ answer_count          │
│  6 │ score                 │
│  7 │ last_activity_date    │
│  8 │ creation_date         │
│  9 │ question_id           │
│ 10 │ content_license       │
│ 11 │ link                  │
│ 12 │ title                 │
│ 13 │ posted_by_collectives │
│ 14 │ closed_date           │
│ 15 │ closed_reason         │
│ 16 │ last_edit_date        │
│ 17 │ accepted_answer_id    │
╰────┴───────────────────────╯

# Which questions were about Python?
> $res.items | filter {|question| "python" in $question.tags} |  select title link
╭───┬──────────────────────────────────────────────────────────────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ # │                                title                                 │                                                       link                                                       │
├───┼──────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 0 │ Python Mypy fails on Decorator                                       │ https://stackoverflow.com/questions/76766815/python-mypy-fails-on-decorator                                      │
│ 1 │ How do I find the key with the highest value in a python dictionary? │ https://stackoverflow.com/questions/76766810/how-do-i-find-the-key-with-the-highest-value-in-a-python-dictionary │
│ 2 │ How to insert a Python script into a website with DJango?            │ https://stackoverflow.com/questions/76766795/how-to-insert-a-python-script-into-a-website-with-django            │
│ 3 │ Beautifulsoup append ignores namespace (xml)                         │ https://stackoverflow.com/questions/76766794/beautifulsoup-append-ignores-namespace-xml                          │
│ 4 │ Http api not giving updated response                                 │ https://stackoverflow.com/questions/76766744/http-api-not-giving-updated-response                                │
│ 5 │ What type hint should I use to specify a set of functions            │ https://stackoverflow.com/questions/76766740/what-type-hint-should-i-use-to-specify-a-set-of-functions           │
│ 6 │ can someone explain this append() part of a for loop?                │ https://stackoverflow.com/questions/76766707/can-someone-explain-this-append-part-of-a-for-loop                  │
│ 7 │ How to make it faster?                                               │ https://stackoverflow.com/questions/76766684/how-to-make-it-faster                                               │
│ 8 │ Changing Pandas Period to another frequency                          │ https://stackoverflow.com/questions/76766650/changing-pandas-period-to-another-frequency                         │
│ 9 │ PFX certificate with Selenium with python                            │ https://stackoverflow.com/questions/76766634/pfx-certificate-with-selenium-with-python                           │
╰───┴──────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

For comparison, some Bash/curl/jq equivalents:

> res=`curl -o - "https://api.stackexchange.com/questions?pagesize=100&site=stackoverflow&sort=creation" | gunzip`

> echo $res | jq '.items[0:5][].title'
"Strange data prepending incoming WebSocket data. What is the cause?"
"Why does nothing happen when i try to multiple two variables?"
"How can I test functions that node-redis using vitest?"
"Create a Piece-Wise Regression in ggplot2 (R) with multiple &#39;knots&#39;"
"feeding continuous stdin using bash script"

> echo $res | jq '.quota_remaining'
143

> echo $res | jq 'keys'
[
  "has_more",
  "items",
  "quota_max",
  "quota_remaining"
]

# Note that this is slightly different from Nushell, in that Nushell will show *all*
# columns from all results, whereas this will only show the keys from the first result
> echo $res | jq '.items[0] | keys'
[
  "answer_count",
  "content_license",
  "creation_date",
  "is_answered",
  "last_activity_date",
  "link",
  "owner",
  "post_state",
  "question_id",
  "score",
  "tags",
  "title",
  "view_count"
]

# Which questions were about Python?
> echo $res | jq -r '.items[] | select(.tags[] | contains ("python")) | .title, .link'
feeding continuous stdin using bash script
https://stackoverflow.com/questions/76766988/feeding-continuous-stdin-using-bash-script
Dijkstra&#39;s algorithm while moving through a maze
https://stackoverflow.com/questions/76766972/dijkstras-algorithm-while-moving-through-a-maze
numba: parallelize a nested for loop with a cartopy function in it
https://stackoverflow.com/questions/76766960/numba-parallelize-a-nested-for-loop-with-a-cartopy-function-in-it
How to avoid lack of convergence while using PassiveAggressiveClassifier for online learning?
https://stackoverflow.com/questions/76766958/how-to-avoid-lack-of-convergence-while-using-passiveaggressiveclassifier-for-onl
Opencv camera is available ret returns true, but frame is still empty, is there any fix?
https://stackoverflow.com/questions/76766956/opencv-camera-is-available-ret-returns-true-but-frame-is-still-empty-is-there
Remove pushpin on Bing Maps API using Python requests
https://stackoverflow.com/questions/76766949/remove-pushpin-on-bing-maps-api-using-python-requests
Why key parameters in Python has to be &quot;so complicated&quot;?
https://stackoverflow.com/questions/76766948/why-key-parameters-in-python-has-to-be-so-complicated
Plotting points on a HSV color wheel
https://stackoverflow.com/questions/76766947/plotting-points-on-a-hsv-color-wheel
Solving Sudoku with Gurobipy for Inequality
https://stackoverflow.com/questions/76766946/solving-sudoku-with-gurobipy-for-inequality
Solving Sudoku with Gurobipy for Inequality
https://stackoverflow.com/questions/76766946/solving-sudoku-with-gurobipy-for-inequality
How to open files outside of the django project?
https://stackoverflow.com/questions/76766934/how-to-open-files-outside-of-the-django-project
how to create kedro catalog entry + custom DataSet init method with values from credentials.yml
https://stackoverflow.com/questions/76766933/how-to-create-kedro-catalog-entry-custom-dataset-init-method-with-values-from
Adding a tkinter scrollbar to a treeview without any space
https://stackoverflow.com/questions/76766918/adding-a-tkinter-scrollbar-to-a-treeview-without-any-space
Pandas Ethnicity Count/Percentage
https://stackoverflow.com/questions/76766907/pandas-ethnicity-count-percentage
SQLite3 can&#39;t open database in Flask server
https://stackoverflow.com/questions/76766884/sqlite3-cant-open-database-in-flask-server
How to display a variable from a different file to the screen with PyQt5
https://stackoverflow.com/questions/76766870/how-to-display-a-variable-from-a-different-file-to-the-screen-with-pyqt5
compare two file and get only diffe name
https://stackoverflow.com/questions/76766869/compare-two-file-and-get-only-diffe-name
Why does my pygbag pygame application have laggy sound?
https://stackoverflow.com/questions/76766845/why-does-my-pygbag-pygame-application-have-laggy-sound


1 Answer 1

1

An example of handling results with multiple pages (taken from my Stack Overflow answer - Refer to that answer for the full explanation):

mut questions = []
for page in 1.. {
  print $"Retrieving page ($page)"
  let res = http get $"https://api.stackexchange.com/2.3/questions?fromdate=1648771200&todate=1648944000&order=asc&sort=creation&site=askubuntu&pagesize=100&page=($page)"
  $questions = ( $questions | append $res.items )

  if not $res.has_more { break } # "until" condition
}

Then work with the data as in the examples in the main question. E.g.:

$questions | where view_count > 100 and view_count < 110 | select view_count title link

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .