| Deutsch English Français Italiano |
|
<v99fv3$33qsp$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Lawrence D'Oliveiro <ldo@nz.invalid>
Newsgroups: comp.unix.shell
Subject: JSON And The Command Line
Date: Sun, 11 Aug 2024 04:50:44 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 63
Message-ID: <v99fv3$33qsp$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 11 Aug 2024 06:50:44 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="cbdc2a7fc498ad606df83c52bd2e8b1d";
logging-data="3271577"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vozDmRYxlpANrmORQnAJp"
User-Agent: Pan/0.159 (Vovchansk; )
Cancel-Lock: sha1:vNMCcgUilRtBRCFx0VT7bumu3U4=
Bytes: 2774
Quite a few Linux utilities have the option to format their output as
JSON, for easy subsequent processing with commands like jq
<https://manpages.debian.org/1/jq.1.en.html>.
For example, here’s what part of the output from “lscpu -J” looks like
on one of my machines:
{
"lscpu": [
{
"field": "Architecture:",
"data": "x86_64"
},{
"field": "CPU op-mode(s):",
"data": "32-bit, 64-bit"
},{
"field": "Address sizes:",
"data": "36 bits physical, 48 bits virtual"
},{
"field": "Byte Order:",
"data": "Little Endian"
},{
"field": "CPU(s):",
"data": "8"
},{
...
}
That’s a bit verbose, isn’t it? jq can still select information from
here, but wouldn’t it look a bit neater if instead of
{
"field": «key»,
"data": «value»
}
you had the more concise
{«key»: «value»}
?
Actually it’s quite easy to do this transformation. jq has built-in
functions “to_entries” and “from_entries” which can convert between
the verbose and concise layouts, but it only recognizes names like
“key” and “value”, not “field” and “data”.
Never mind: it’s easy enough to remap the names. Putting it all
together, including pulling out the lone “lscpu” field from the
original struct, here is the JSON processing pipeline:
lscpu -J | jq '.lscpu | map({"key" : .field, "value" : .data}) | from_entries'
And the initial part of the corresponding output:
{
"Architecture:": "x86_64",
"CPU op-mode(s):": "32-bit, 64-bit",
"Address sizes:": "36 bits physical, 48 bits virtual",
"Byte Order:": "Little Endian",
"CPU(s):": "8",
...
}