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?

1 Answer 1

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 ) ) )'
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.