0

Please help me to convert SQL query Where Clause Condition into JSON Object?

1 Answer 1

2
**Here is Solution:~**

<script>

function handle_space_of_string(query){
    start = false;
    t = false;
    target_str = "";
    for (var i =0; i < query.length; i++){
        if(query[i] == "'" || query[i] == '"'){
            if(t == false){
                t = true;
                start = true;
            }else{
                t = false;
                start = false;
                
            }
        }
        if (start == true){
            if (query[i] == ' '){
                target_str += '~~!~~';
            }else{
                target_str += query[i];
            }
        }else{

            target_str += query[i];
        }
    }
    return target_str;
}

function parser_string(str){
    if (isNaN(str)){
        temp_str = str;
        ret_str = "";
        for (var i = 0; i < temp_str.length; i++){
            if ((temp_str[i]) == '"' ||  temp_str[i] == "'" || temp_str[temp_str.length] == '"' ||  temp_str[temp_str.length] == "'"){
                continue;
            }
            if (temp_str[i] == ' '){
                ret_str += '~~!~~';
            }else{
                ret_str += temp_str[i];
            }
        }
            
        return ret_str.replaceAll("~~!~~", ' ');
    }else{
        return str;
    }
}

function get_operator(op){
        operators = {
            "=":"eq",
            "==":"de",
            "!=":"ne",
            "<":"lt",
            ">":"gt",
            "<=":"le",
            ">=":"ge",
            "in": "in",
            "notin": "ni",
            "=":"is" ,
            "is":"is" ,
            "isnot":"isn",
            "!=":"isn",
        }
        return operators[op]
    }
function query_to_json_object(query){
    query = handle_space_of_string(query)
    query_tmp = query.split(" ")
    query = []
    for( var j = 0; j < query_tmp.length; j++){
        if (query_tmp[j] != ""){
            query.push(query_tmp[j].toLowerCase())
        }
    }
    query = query.join(" ")
    query = query.replace("is not", "isnot")
    query = query.replace("not in", "notin")

    query = query.split(" ")
    query.push(query[3])

    filters = []
    temp = []
    k = 0
    for (var i = 0; i < query.length+1; i++){
        temp[k] = query[i]
        k++
        if (i == query.length){
            break;
        }
        if(query[i+1].toLowerCase() == "and"){

            filters.push(
                {
                    "column_hash": parser_string(temp[0]),
                    "operator": get_operator(temp[1]),
                    "value": parser_string(temp[2]),

                }
            )
            temp = []
            k = 0
            i = i + 1
        }
        else if((query[i+1]).toLowerCase() == "or"){
            filters.push(
                [
                    {
                        "column_hash": parser_string(temp[0]),
                        "operator": get_operator(temp[1]),
                        "value": parser_string(temp[2]),
                    }
                ]
            )
            temp = []
            k = 0
            i = i + 1
        }
        
    }
    return filters
}
query = `Col1 is  Not 10 and  Col2 <  'Time  of ' AND  Col3 <= 8 AnD Col3 <= "Time1  2of" aNd Col3 <= 9`

document.write(query_to_json_object(query))
console.log(query_to_json_object(query))
</script>
Sign up to request clarification or add additional context in comments.

Comments

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.