Posted: . At: 8:37 AM. This was 7 months ago. Post ID: 18660
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



How to easily use jq to parse JSON and read multiple values.


I was interested in reading multiple values from a JSON file and I have found out how to do this easily.

Just use jq like this.

curl -s https://www.news.com.au/wp-json/wp/v2/posts/ | jq -r '.[] | .title,.excerpt,.link'

This will allow the reading of multiple values from a JSON file.

The result is shown below.

(jcartwright@localhost) 192.168.1.5 ~  $ curl -s https://www.news.com.au/wp-json/wp/v2/posts/ | jq -r '.[] | .title,.excerpt,.link'
{
  "rendered": "US tycoon loses $12.4m on a single bet"
}
{
  "rendered": "<p>Mattress Mack’s $A12.4 million wagers on the Astros to win the World Series fell by the wayside Monday night thanks to another offensive onslaught by the Texas Rangers.</p>\n",
  "protected": false
}
https://www.news.com.au/sport/american-sports/mlb/mattress-mack-loses-small-fortune-after-houston-astros-knocked-out/news-story/65e34e488404b81177e526b3086d73c0
{
  "rendered": "NN-REC/SPORT/MASTHEADS/NCAU-TOP-SPORT (192655)"
}
{
  "rendered": "",
  "protected": false
}
https://www.news.com.au/news/nnrecsportmastheadsncautopsport-192655/collection/66a6c6f8d4ae2cd33c68307991b2d500
{
  "rendered": "NC-NN-REC/NEWS.COM.AU HOME TECHNOLOGY MODULE (171523)"
}
{
  "rendered": "",
  "protected": false
}
https://www.news.com.au/news/ncnnrecnewscomau-home-technology-module-171523/collection/9035b9b871a198a4f9574ddaa13e20e6
{
  "rendered": "NC-NN-REC/NEWS Rec National and World (192348)"
}
{
  "rendered": "",
  "protected": false
}
https://www.news.com.au/news/ncnnrecnews-rec-national-and-world-192348/collection/6c5b2289011c1fc71299a41058646127

And another example.

(jcartwright@localhost) 192.168.1.5 ~  $ curl -s https://www.news.com.au/wp-json/wp/v2/posts/ | jq '.[] | .title,.date,.status,.id,.link'
{
  "rendered": "NC-NN-REC/NEWS.COM.AU HOME LIFESTYLE MODULE (175427)"
}
"2023-10-25T07:25:15"
"publish"
124
"https://www.news.com.au/news/ncnnrecnewscomau-home-lifestyle-module-175427/collection/7a8b3a68a3b11bb534070f6086020a33"
{
  "rendered": "UN chief alleges law violations in Gaza, angering Israel"
}
"2023-10-25T07:17:16"
"publish"
17942935
"https://www.news.com.au/breaking-news/un-chief-alleges-violations-of-law-in-gaza-angering-israel/news-story/f33d0ba1b76ae3fa749b1d50d1841017"
{
  "rendered": "NC-NN-REC/NEWS.COM.AU HOME ENTERTAINMENT MODULE (172515)"
}
"2023-10-25T07:17:06"
"publish"
14606782
"https://www.news.com.au/news/ncnnrecnewscomau-home-entertainment-module-172515/collection/fd9f21b4d29ecb99c6169af658c9d925"
{
  "rendered": "NC-REC/SPP/FEATURES (170229)"
}
"2023-10-25T07:17:01"
"publish"
125
"https://www.news.com.au/news/ncrecsppfeatures-170229/collection/b46b13fffb7142faea39e9bf3fba4a95"
{
  "rendered": "Julia Morris’ near-nude stunt on new show"
}
"2023-10-25T07:16:00"
"publish"
17944291
"https://www.news.com.au/entertainment/tv/streaming/julia-morris-jokes-she-wont-be-asked-back-next-year-on-celebrity-gogglebox-after-onset-stunt/news-story/487da57fcba356398a454890d2976bbe"
{
  "rendered": "NC-NN-REC/NEWS.COM.AU HOME WORLD MODULE"
}
"2023-10-25T07:14:05"
"publish"
126
"https://www.news.com.au/news/ncnnrecnewscomau-home-world-module/collection/c57b166d9131dcf96b9b1d9f232aeaa2"

This shows how easy it is to read JSON and parse multiple values.

To output the JSON data in plain text, one entry per line, you can use the -r or --raw option with jq. By using the -r option, jq will output the raw text of the selected fields, and we’re also using \n to add newline characters between each field, so each entry will be on a separate line. This will give you the output in plain text with one entry per line.

(jcartwright@localhost) 192.168.1.5 ~  $ curl -s https://www.news.com.au/wp-json/wp/v2/posts/ | jq -r '.[] | "\(.title)\n\(.date)\n\(.status)\n\(.link)"'
{"rendered":"‘Yellow pillow’ all men own replusing women"}
2023-10-25T07:46:00
publish
https://www.news.com.au/lifestyle/real-life/news-life/not-my-fault-women-repulsed-by-mans-gross-bedroom-admission/news-story/43043f9b46d66ee7721ae880e57dd9e5
{"rendered":"NC-NN-REC/NEWS.COM.AU TOP STORIES (174652)"}
2023-10-25T07:45:37
publish
https://www.news.com.au/news/ncnnrecnewscomau-top-stories-174652/collection/6b4763d854a2308e2a767518cb5372b7
{"rendered":"NC-NN-REC/NEWS.COM.AU HOME WORLD MODULE"}
2023-10-25T07:42:57
publish
https://www.news.com.au/news/ncnnrecnewscomau-home-world-module/collection/c57b166d9131dcf96b9b1d9f232aeaa2
{"rendered":"NN-REC/SPORT/MASTHEADS/NCAU-TOP-SPORT (192655)"}
2023-10-25T07:42:15
publish
https://www.news.com.au/news/nnrecsportmastheadsncautopsport-192655/collection/66a6c6f8d4ae2cd33c68307991b2d500
{"rendered":"NC-NN-REC/NEWS Rec National and World (192348)"}
2023-10-25T07:41:45
publish
https://www.news.com.au/news/ncnnrecnews-rec-national-and-world-192348/collection/6c5b2289011c1fc71299a41058646127
{"rendered":"Horrifying footage shows Hersh Goldberg-Polin moments after arm was blown off by Hamas"}
2023-10-25T07:39:09
publish
https://www.news.com.au/world/horrifying-footage-shows-hersh-goldbergpolin-moments-after-arm-was-blown-off-by-hamas/video/34052c94fc3c44ab144128aee814b796

But a simpler way is to just do it like this.

(jcartwright@localhost) 192.168.1.5 ~  $ curl -s https://www.news.com.au/wp-json/api/weather/2000 | jq -r '.metadata'
{
  "sector": "weather",
  "title": "Weatherzone",
  "provider_name": "Weatherzone",
  "provider_url": "http://www.weatherzone.com.au",
  "project_version": "5.04",
  "revision": 30518,
  "last_commit": "2023-06-22 15:16:00 +1000 (Thu, 22 Jun 2023)",
  "create_time": "2023-10-25T08:18:25+1100",
  "create_tz": "AEDT",
  "create_time_utc": "2023-10-24T21:18:25Z",
  "validity": "2023-10-25"
}

This shows how easy this is. Parsing JSON can be annoying at times, but this should help out a bit.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.