I have the following statement:
column1==1 && 'column2'=='value' || year == 2006 || ('column2'=='value1' && 'column2'!='value2')
which is pass to me like the JSON
below:
{
"type": "ExpressionStatement",
"expression": {
"type": "LogicalExpression",
"operator": "||",
"left": {
"type": "LogicalExpression",
"operator": "||",
"left": {
"type": "LogicalExpression",
"operator": "&&",
"left": {
"type": "BinaryExpression",
"operator": "==",
"left": {
"type": "Identifier",
"name": "column1"
},
"right": {
"type": "Literal",
"value": 1,
"raw": "1"
}
},
"right": {
"type": "BinaryExpression",
"operator": "==",
"left": {
"type": "Literal",
"value": "column2",
"raw": "'column2'"
},
"right": {
"type": "Literal",
"value": "value",
"raw": "'value'"
}
}
},
"right": {
"type": "BinaryExpression",
"operator": "==",
"left": {
"type": "Identifier",
"name": "year"
},
"right": {
"type": "Literal",
"value": 2006,
"raw": "2006"
}
}
},
"right": {
"type": "LogicalExpression",
"operator": "&&",
"left": {
"type": "BinaryExpression",
"operator": "==",
"left": {
"type": "Literal",
"value": "column2",
"raw": "'column2'"
},
"right": {
"type": "Literal",
"value": "value1",
"raw": "'value1'"
}
},
"right": {
"type": "BinaryExpression",
"operator": "!=",
"left": {
"type": "Literal",
"value": "column2",
"raw": "'column2'"
},
"right": {
"type": "Literal",
"value": "value2",
"raw": "'value2'"
}
}
}
}
}
I want to extract from this JSON
string, only the binary expression operands. For example, for column1==1
I need only this part of the JSON
string:
"left": {
"type": "Identifier",
"name": "column1"
},
"right": {
"type": "Literal",
"value": 1,
"raw": "1"
}
Having that string I can get the column name and the comparison value. As the JSON
format string is consistent, I can use zero-width positive lookbehind assertion and check for "type": "BinaryExpression",
value. Then I need to get all data to the second }
.
Is there a way to say with regex to get everything to the second occurrence of }
?
Note:
- I know it will be better to use some
JSON
tostring
parser, but I am not allowed (I am working in the context of SQL Server). - I know in SQL Server 2016 there is native JSON support, but some of the database are on earlier version.
- I need pure regex solution, as in the context of the T-SQL I am not able to use .net like this one for example.