Search code examples
jsonstringselectjqany

Select JSON values with special characters


I am looking to detect anomalies in my JSON values. Here's an example of the data queries via jq

"2014-03-26 01:58:00"
"9019549360"
"109092812_20150626"
"134670164"
""
"97695498"
"680561513"

I would like to display all the values that contain a - or a _ or is blank.

In other words, I'd like to display the following output

"2014-03-26 01:58:00"
"109092812_20150626"
""

Now, I have tried the following:

select (. | contains("-","_"," "))'

This appears to work, but in order to make it more robust, I'd like to expand this to include all special characters.


Solution

  • Your query won't detect empty strings, and will possibly emit the same string more than once. It would be easier to use test, e.g.:

    select( length==0 or test("[-_ ]") )
    

    Note also that the preliminary '.' in your query is unnecessary.

    Addendum

    From one of the comments, it awould appear that you will want to specify "[^a-zA-Z0-9]" or similar as the argument of test.