根据路径和正则表达式过滤JSON

时间:2019-07-02 19:03:04

标签: javascript json regex

我希望能够让用户能够指定路径和有效的正则表达式,并基于此返回过滤的JSON。

除了不知道如何动态获取路径结果外,我认为我几乎有解决方案。这是我的代码:

function getResult(jsonObject, pathText, regexToCheck) {
    var pathArr = pathText.split(".")
    var jsonPath = ''
    console.log('start...')
    for(var key in pathArr){
        var addPath = "['"+ pathArr[key] +"']"
        jsonPath += addPath
    }
    result = jsonObject[jsonPath]
    return result.match(new RegExp(regexToCheck, 'g'), match)
}

function filterBy (json, path, regexToCheck){
    var parseJSON = json
    var filterResult = [];
    for(var obj in parseJSON){
        var result = getResult(parseJSON[obj], path, regexToCheck)
        console.log(result)
        if (result == true){
            filteredResult.push(parseJSON[obj])
        }
    }
    return filterResult
}

filterBy(json, path, regexToCheck)

我想前面提到的是让用户指定路径和正则表达式,例如var path = 'configurationId.id'var regexToCheck = /^[4]/并在下面给出测试数据

var json = [{
    "configurationId": {
      "id": "7000",
      "displayName": "7000",
      "uri": "/configuration/users/7000"
    },
    "licenseProperties": {
      "hasClientAccess": true
    },
    "roles": {
      "actualValue": [{
          "id": "Agent",
          "displayName": "Agent",
          "uri": "/configuration/roles/Agent"
        },
        {
          "id": "SMS",
          "displayName": "SMS",
          "uri": "/configuration/roles/SMS"
        }
      ]
    }
  }, {
    "configurationId": {
      "id": "7001",
      "displayName": "7001",
      "uri": "/configuration/users/7001"
    },
    "licenseProperties": {
      "hasClientAccess": true
    },
    "roles": {
      "actualValue": [{
          "id": "Agent",
          "displayName": "Agent",
          "uri": "/configuration/roles/Agent"
        },
        {
          "id": "SMS",
          "displayName": "SMS",
          "uri": "/configuration/roles/SMS"
        }
      ]
    }
  }, {
    "configurationId": {
      "id": "7002",
      "displayName": "7002",
      "uri": "/configuration/users/7002"
    },
    "licenseProperties": {
      "hasClientAccess": true
    },
    "roles": {
      "actualValue": [{
          "id": "Agent",
          "displayName": "Agent",
          "uri": "/configuration/roles/Agent"
        },
        {
          "id": "SMS",
          "displayName": "SMS",
          "uri": "/configuration/roles/SMS"
        }
      ]
    }
  }, {
    "configurationId": {
      "id": "4003",
      "displayName": "4003",
      "uri": "/configuration/users/4003"
    },
    "licenseProperties": {
      "hasClientAccess": true
    },
    "roles": {
      "actualValue": [{
          "id": "Agent",
          "displayName": "Agent",
          "uri": "/configuration/roles/Agent"
        },
        {
          "id": "SMS",
          "displayName": "SMS",
          "uri": "/configuration/roles/SMS"
        }
      ]
    }
  }];

让结果返回,

{
    "configurationId": {
      "id": "4003",
      "displayName": "4003",
      "uri": "/configuration/users/4003"
    },
    "licenseProperties": {
      "hasClientAccess": true
    },
    "roles": {
      "actualValue": [{
          "id": "Agent",
          "displayName": "Agent",
          "uri": "/configuration/roles/Agent"
        },
        {
          "id": "SMS",
          "displayName": "SMS",
          "uri": "/configuration/roles/SMS"
        }
      ]
    }

由于我的正则表达式仅检查ID是否以4开头。因此,还必须注意,它必须与嵌套的JSON一起使用,因此必须指定路径。因此,要重申我的代码失败的地方,基本上是在这里:result = jsonObject[jsonPath]

1 个答案:

答案 0 :(得分:0)

好吧,最后它变成了一个循环,您在该循环中基于先前提供的数组沿路径行进。这是我的完整解决方案,并且为您每次要遍历对象内部的数组

修复了一个错误
export function filterBy (json, path, regexToCheck) {
  var parseJSON = json
  var filteredResult = []
  for (var obj in parseJSON) {
    var result = getResult(parseJSON[obj], path, regexToCheck)
    if (result == true) {
      filteredResult.push(parseJSON[obj])
    }
  }
  return filteredResult
}

function getResult (jsonObject, pathText, regexToCheck) {
  var pathArr = pathText.split('.')
  var testObj
  var testResult
  var foundmatch = false
  for (var item in pathArr) {
    // Handles Arrays
    if (Object.prototype.toString.call(jsonObject) == '[object Array]') {
      jsonObject.forEach(function (obj) {
        testObj = ''
        testResult = ''
        testObj = obj[pathArr[item]]
        testResult = testObj.match(new RegExp(regexToCheck, 'g'))
        if (testResult != null) foundmatch = true
      })
    } else {
      testResult = ''
      jsonObject = jsonObject[pathArr[item]]
    }
  }

  if (Object.prototype.toString.call(jsonObject) != '[object Array]') {
    jsonObject = jsonObject.match(new RegExp(regexToCheck, 'g'))
    if (jsonObject != null) foundmatch = true
  }

  return foundmatch
}