Home > Back-end >  What is the JSONPATH operator for DOES NOT MATCH regular expression?
What is the JSONPATH operator for DOES NOT MATCH regular expression?

Time:06-25

I have the following item in Zabbix, with the current PreProcessing JSONPath:

Item key: vfs.fs.get 
Preprocessing JSONPath script: $.[?(@.fstype =~ '{$FSTYPE.MATCHES}')]

The structured JSON list that it generates is the following:

[{
    "fsname": "/",
    "fstype": "ext4",
    "bytes": {
        "total": 30027956224,
        "free": 9868255232,
        "used": 18610745344,
        "pfree": 34.650989,
        "pused": 65.349011
    },
    "inodes": {
        "total": 1872304,
        "free": 1478095,
        "used": 394209,
        "pfree": 78.945246,
        "pused": 21.054754
    }
}, {
    "fsname": "/mnt/dados",
    "fstype": "ext4",
    "bytes": {
        "total": 157451501568,
        "free": 149319151616,
        "used": 62930944,
        "pfree": 99.957872,
        "pused": 0.042128
    },
    "inodes": {
        "total": 9830400,
        "free": 9830389,
        "used": 11,
        "pfree": 99.999888,
        "pused": 0.000112
    }
}]

So, currently it gets only items that have fstype "ext4" and a few others (Like ext3, etc.) But in order to be more effective, i need to include another PreProcessing step that is one called "DOES NOT MATCH what is in regular expression."

So, the JSONPath script would be something like (Meaning "Is different from the regular expression", so items with empty values, weird numbers and space values will be ignored):

"$.[?(@.fstype != =~ '{$FSTYPE.DOES_NOT_MATCH}')]" 

But i tried doing the following on a JSONPATH Evaluator app and i got the following error:

JSONPath: $.[?(@.fstype != =~ '^\s$')]
Error: jsonPath: Unexpected token '=': _$_v.fstype != =~ '^\s$'

The following JSONPath works but it does the opposite, it gets the items with empty values, weird numbers and space values and ignores others:

$.[?(@.fstype =~ '^\s$')]

Can someone help me? What am i missing?

Thank you...

Edit:

Progression: I was able to create three PreProcessing steps, with the help of gregsdennis below.

JSONPath: $.[?(@.fstype =~ '^(ext4)$')]
JSONPath: $.[?(@.fsname =~ '. ')]
JSONPath: $.[?(!(@.fsname =~ '^(/dev|/sys|/run|/proc|. /shm$)'))]

All steps above passed. But for some reason i am not able to negate the following expression:

$.[?(!(@.fstype =~ '^\s$'))]

Accordingly to RegEx documentation, the expression above means:

^ asserts position at start of the string
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

But its returning me the following error:

"Cannot extract value from json by path "$.[?(!(@.fstype =~ '^\s$'))]": unsupported construct in jsonpath starting with: "'^\s$'))]"

Edit 2:

I was able to fix the "unsupported construct in jsonpath starting with" error by escaping the backslash character. So:

$.[?(!(@.fstype =~ '^\s$'))]

CodePudding user response:

There's not a single operator, but you should be able to negate the entire expression.

$.foo[?(!(@.bar =~ '[a-z0-9] '))]
  • Related