Re: trying to parse lines from an awkwardly formatted HAR file ...
On Sat, Mar 23, 2024 at 08:18:46AM +0000, mgrant@grant.org wrote:
> jq is an amazing tool, it's a full fledged programming language. You just
> need to continue concatenating your desired output. You might even find you
> can do what you want all inside a jq script instead of what you're doing.
> Consider writing a jq script with the first line of the script #!/usr/bin/jq
Yeah. This. If you have to process JSON inputs in a bash script, use jq.
Do not attempt to roll your own JSON parser. That work has already been
done, and besides that, bash is a *terrible* language in which to write
a parser.
Here's an *incredibly* brief glimpse. It can do so much more:
hobbit:~$ json='{"foo":"bar", "names": ["Alice","Bob"], "phone": {"Alice":"555-1234", "Bob":"555-2345"}}'
hobbit:~$ printf %s "$json" | jq
{
"foo": "bar",
"names": [
"Alice",
"Bob"
],
"phone": {
"Alice": "555-1234",
"Bob": "555-2345"
}
}
hobbit:~$ printf %s "$json" | jq .foo
"bar"
hobbit:~$ printf %s "$json" | jq -r .foo
bar
hobbit:~$ printf %s "$json" | jq -r .phone.Alice
555-1234
hobbit:~$ printf %s "$json" | jq -r '.names[1]'
Bob
Reply to: