News for hackers. Stuff that matters.
Re:Htmlq: like jq, but for html by harperlee on Tuesday September 07, 2021 @03:52AM (#28442149)
Nice!

This is the kind of obvious tool that once it exists, you can’t really grok the fact it did not earlier, and that it took until now to exist.

Re:Htmlq: like jq, but for html by dmos62 on Tuesday September 07, 2021 @04:03AM (#28442206)
> grok

A good opportunity to introduce `gron` to those unfamiliar!

    ▶ gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" | fgrep "commit.author"
    json[0].commit.author = {};
    json[0].commit.author.date = "2016-07-02T10:51:21Z";
    json[0].commit.author.email = "[email protected]";
    json[0].commit.author.name = "Tom Hudson";

https://github.com/tomnomnom/gron
Re:Htmlq: like jq, but for html by rsync on Tuesday September 07, 2021 @04:08AM (#28442252)
"A good opportunity to introduce `gron` to those unfamiliar!"

Thank you - appreciated.

I haven't done much work with json but have had reasons recently to do so - and I immediately saw how difficult it was to pipeline to grep ...

But what I still don't understand is that some json outputs I see have multiple values with the exact same name (!) and that still seems "un-grep-able" to me ...

What am I missing ?

Re:Htmlq: like jq, but for html by dotancohen on Tuesday September 07, 2021 @05:49AM (#28442853)

  > But what I still don't understand is that some json
  > outputs I see have multiple values with the exact same name
This is neither explicitly allowed nor explicitly forbidden by the JSON spec. It is implementation dependent upon how to handle - does one value override the other? Should they be treated as an array?

In practice, this situation is usually carefully avoided by services that produce JSON. If you are interfacing with a service that does produce duplicate values, I'd be interested in seeing it for curiosity's sake. If you are writing a service and this is the output, then I implore you to reconsider!

Re:Htmlq: like jq, but for html by dmos62 on Tuesday September 07, 2021 @05:13AM (#28442639)
You might be missing a change in index: `obj[0].prop` vs `obj[1].prop`. Or, your JSON might have the same property defined multiple times: `{a:1, a:2}` (though I'm not sure how gron handles that situation).
Re:Htmlq: like jq, but for html by lvncelot on Tuesday September 07, 2021 @06:03AM (#28442933)
> (though I'm not sure how gron handles that situation).

It seems both gron and jq only use the value that has been defined last:

  ~  echo '{"a":1,"a":2}' | gron                                                                                                                                   
  json = {};
  json.a = 2;
  ~  echo '{"a":1,"a":2}' | jq                                                                                                                                    
  {
    "a": 2
  }
Re:Htmlq: like jq, but for html by croon on Tuesday September 07, 2021 @04:12AM (#28442270)
The json output likely contains multiple objects. Can you request more specifically the object(s) you need and grep on that?
Re:Htmlq: like jq, but for html by natrys on Tuesday September 07, 2021 @05:48AM (#28442847)
It's not novel obviously. I have been using pup[1] for years. And xidel[2] is probably older.

[1] https://github.com/ericchiang/pup

[2] https://github.com/benibela/xidel

Re:Htmlq: like jq, but for html by ptwt on Tuesday September 07, 2021 @04:46AM (#28442491)
It did write it a few years ago.

https://github.com/plainas/tq

Re:Htmlq: like jq, but for html by mcnesium on Wednesday September 08, 2021 @01:32AM (#28453413)
and I use it almost every day. It's great, thank you very much!
Re:Htmlq: like jq, but for html by matsemann on Tuesday September 07, 2021 @04:07AM (#28442243)
There are already tools for xpath, but using css selectors is much more aligned with what I write every day, so that's nice.
Re:Htmlq: like jq, but for html by harperlee on Tuesday September 07, 2021 @04:10AM (#28442266)
Yes, and awk and others. I meant something semantically closer to the need, with css selectors.
Re:Htmlq: like jq, but for html by mcovey on Wednesday September 08, 2021 @09:44AM (#28456848)
I have been using hxselect from the html-xml-utils package to do this for many, many years.

It doesn't handle malformed HTML that well but can be coaxed into working about 90% of the time, with the help of the other included package hxclean or something like html-tidy.