1

I often get sent HAR files (which are JSON) sent to me that look like this:

{
    "log": {
        "entries" : [
            {
                "request" : {
                    "url" : "test.css"
                }
            },
            {
                "request" : {
                    "url" : "test.ok"
                }
            },
            {
                "request" : {
                    "url" : "test.font"
                }
            },
            {
                "request" : {
                    "url" : "ok"
                }
            }
        ]
    }
}

I don't care about requests that contain URLs for fonts, CSS, or JavaScript. So, I'd like to remove these requests using jq. Given the answer from @iain-samuel-mclean-elder about filtering and maintaining the JSON structure of the input, I would expect something like this to work:

jq '[ .[] | select(.log.entries[].request.url | test("\\.(js|css|font)") | not) ]' < MyGoodHarFile.json

This, however, produces the error:

jq: error (at <stdin>:25): Cannot iterate over null (null)

What am I doing wrong? How can I create a valid HAR file excluding requests for these certain matching URLs using jq?

peak
  • 105,803
  • 17
  • 152
  • 177
Travis Spencer
  • 2,231
  • 16
  • 26

1 Answers1

4

You should be really careful where and how select statements are used. Avoiding the error of incorrect parent path .[] in your original filter

[select(.log.entries[].request.url | test("\\.(js|css|font)") | not)] 

will produce the whole input twice because the filter asserts true for two of your objects because select() replicates the entire input on true condition.

By virtue of doing .log.entries|=, your input is now only on the array of objects which when asserted true through regex are retained and the others excluded.

jq '.log.entries |= ( map ( select ( .request.url | test("\\.(js|css|font)") |not ) ) )'
Inian
  • 80,270
  • 14
  • 142
  • 161
  • Very much appreciated, @inian! I removed the extra `(` before map to try to keep it as simple as possible. Works perfectly though. Thanks for the explanation and help. – Travis Spencer Apr 06 '20 at 14:01