简体   繁体   中英

javascript json iteration to get key and value

I am trying to iterate the json below, I have got this as an output of an astify of sql parser. Now i want to validate the column type with a DB schema that i have.

{
  "with": null,
  "type": "select",
  "options": null,
  "distinct": null,
  "columns": "*",
  "from": [
    {
      "db": null,
      "table": "TABLE_1",
      "as": null
    }
  ],
  "where": {
    "type": "binary_expr",
    "operator": "AND",
    "left": {
      "type": "binary_expr",
      "operator": "AND",
      "left": {
        "type": "binary_expr",
        "operator": "=",
        "left": {
          "type": "column_ref",
          "table": null,
          "column": "COLUMN_1"
        },
        "right": {
          "type": "string",
          "value": "NORM"
        }
      },
      "right": {
        "type": "binary_expr",
        "operator": "IN",
        "left": {
          "type": "column_ref",
          "table": null,
          "column": "COLUMN_2"
        },
        "right": {
          "type": "expr_list",
          "value": [
            {
              "type": "string",
              "value": "11"
            },
            {
              "type": "string",
              "value": "12"
            },
            {
              "type": "string",
              "value": "13"
            },
            {
              "type": "string",
              "value": "14"
            },
            {
              "type": "string",
              "value": "15"
            },
            {
              "type": "string",
              "value": "16"
            },
            {
              "type": "string",
              "value": "17"
            },
            {
              "type": "string",
              "value": "18"
            },
            {
              "type": "string",
              "value": "42"
            },
            {
              "type": "string",
              "value": "43"
            },
            {
              "type": "string",
              "value": "44"
            },
            {
              "type": "string",
              "value": "45"
            },
            {
              "type": "string",
              "value": "101"
            },
            {
              "type": "string",
              "value": "102"
            },
            {
              "type": "string",
              "value": "103"
            },
            {
              "type": "string",
              "value": "104"
            },
            {
              "type": "string",
              "value": "128"
            },
            {
              "type": "string",
              "value": "129"
            },
            {
              "type": "string",
              "value": "130"
            },
            {
              "type": "string",
              "value": "131"
            },
            {
              "type": "string",
              "value": "159"
            }
          ]
        }
      }
    },
    "right": {
      "type": "binary_expr",
      "operator": "OR",
      "left": {
        "type": "binary_expr",
        "operator": "IS NOT",
        "left": {
          "type": "column_ref",
          "table": null,
          "column": "COLUMN_3"
        },
        "right": {
          "type": "null",
          "value": null
        }
      },
      "right": {
        "type": "binary_expr",
        "operator": "=",
        "left": {
          "type": "column_ref",
          "table": null,
          "column": "COLUMN_4"
        },
        "right": {
          "type": "number",
          "value": 1
        }
      },
      "parentheses": true
    }
  },
  "groupby": null,
  "having": null,
  "orderby": null,
  "limit": null
}

Can you please help me get an output like which will help me identify the column and its type whether its string or not. Basically i need to validate the query whether its mapped against the correct data type while using a var condition.

Output like:

map = <key,value>
<Column_1, String>
<Column_2, String>
<Column_3, INT>
etc.

Parse the JSON into an object using JSON.parse(), then iterate through them while checking if the value is JSON or not.

Check if the value is JSON by using catch on JSON.parse()

To get the object keys for iteration, Use var keys = Object.keys();

@Suzy, please check below code for your reference:

var arr = {
  "with": null,
  "type": "select",
  "options": null,
  "distinct": null,
  "columns": "*",
  "from": [
    {
      "db": null,
      "table": "TABLE_1",
      "as": null
    }
  ],
  "where": {
    "type": "binary_expr",
    "operator": "AND",
    "left": {
      "type": "binary_expr",
      "operator": "AND",
      "left": {
        "type": "binary_expr",
        "operator": "=",
        "left": {
          "type": "column_ref",
          "table": null,
          "column": "COLUMN_1"
        },
        "right": {
          "type": "string",
          "value": "NORM"
        }
      },
      "right": {
        "type": "binary_expr",
        "operator": "IN",
        "left": {
          "type": "column_ref",
          "table": null,
          "column": "COLUMN_2"
        },
        "right": {
          "type": "expr_list",
          "value": [
            {
              "type": "string",
              "value": "11"
            },
            {
              "type": "string",
              "value": "12"
            },
            {
              "type": "string",
              "value": "13"
            },
            {
              "type": "string",
              "value": "14"
            },
            {
              "type": "string",
              "value": "15"
            },
            {
              "type": "string",
              "value": "16"
            },
            {
              "type": "string",
              "value": "17"
            },
            {
              "type": "string",
              "value": "18"
            },
            {
              "type": "string",
              "value": "42"
            },
            {
              "type": "string",
              "value": "43"
            },
            {
              "type": "string",
              "value": "44"
            },
            {
              "type": "string",
              "value": "45"
            },
            {
              "type": "string",
              "value": "101"
            },
            {
              "type": "string",
              "value": "102"
            },
            {
              "type": "string",
              "value": "103"
            },
            {
              "type": "string",
              "value": "104"
            },
            {
              "type": "string",
              "value": "128"
            },
            {
              "type": "string",
              "value": "129"
            },
            {
              "type": "string",
              "value": "130"
            },
            {
              "type": "string",
              "value": "131"
            },
            {
              "type": "string",
              "value": "159"
            }
          ]
        }
      }
    },
    "right": {
      "type": "binary_expr",
      "operator": "OR",
      "left": {
        "type": "binary_expr",
        "operator": "IS NOT",
        "left": {
          "type": "column_ref",
          "table": null,
          "column": "COLUMN_3"
        },
        "right": {
          "type": "null",
          "value": null
        }
      },
      "right": {
        "type": "binary_expr",
        "operator": "=",
        "left": {
          "type": "column_ref",
          "table": null,
          "column": "COLUMN_4"
        },
        "right": {
          "type": "number",
          "value": 1
        }
      },
      "parentheses": true
    }
  },
  "groupby": null,
  "having": null,
  "orderby": null,
  "limit": null
};

function findProp(obj, key, out) {
    var i,
        proto = Object.prototype,
        ts = proto.toString,
        hasOwn = proto.hasOwnProperty.bind(obj);

    if ('[object Array]' !== ts.call(out)) out = [];

    for (i in obj) {
        if (hasOwn(i)) {
            if (i === key) {
                out.push(obj[i]);
            } else if ('[object Array]' === ts.call(obj[i]) || '[object Object]' === ts.call(obj[i])) {
                findProp(obj[i], key, out);
            }
        }
    }

    return out;
}

console.log(findProp(arr, "column"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM