请帮助在根级别和选项数组内部删除嵌套的空元素,并了解选项数组中为什么有空元素.

Input:

[
  {
    "autoNumber": false,
    "byteLength": 18,
    "controllerName": null,
    "createable": true,
    "defaultValue": null,
    "label": "Case ID",
    "name": "Id",
    "nillable": false,
    "picklistValues": []
  },
  {
    "autoNumber": false,
    "byteLength": 18,
    "createable": false,
    "label": "Asset ID",
    "name": "AssetId",
    "picklistValues": [],
    "nillable": false,
    "type": "reference"
  },
  {
    "autoNumber": false,
    "byteLength": 765,
    "createable": true,
    "label": "Case Type",
    "length": 255,
    "name": "Select list",
    "nillable": true,
    "picklistValues": [
      {
        "label": "Mechanical",
        "validFor": null,
        "value": "Mechanical"
      },
      {
        "label": "Electrical",
        "validFor": null,
        "value": "Electrical"
      }
    ],
    "type": "picklist"
  }
]

Jolt-Spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "createable": {
          "true": {
            "@(2,name)": ["[#4].crm_field_identifier", "[#4].target_property"],
            "@(2,nillable)": {
              "true": {
                "#false": "[#6].required"
              },
              "false": {
                "#true": "[#6].required"
              }
            },
            "@(2,picklistValues)": {
              "*": {
                "label": "[#6].options[#2].name",
                "value": "[#6].options[#2].value"
              }
            },
            "@(2,defaultValue)": "[&3].default_value"
          }
        }
      }
    }
  }
]

Current O/P:

[
  {
    "crm_field_identifier": "Id",
    "target_property": "Id",
    "required": "true",
    "default_value": null
  },
  null,
  {
    "crm_field_identifier": "Select list",
    "target_property": "Select list",
    "required": "false",
    "options": [
      null,
      null,
      {
        "name": "Mechanical",
        "value": "Mechanical"
      },
      {
        "name": "Electrical",
        "value": "Electrical"
      }
    ],
    "default_value": null
  }
]

Required O/P:

[
  {
    "crm_field_identifier": "Id",
    "target_property": "Id",
    "required": "true",
    "default_value": null
  },
  {
    "crm_field_identifier": "Select list",
    "required": "false",
    "options": [
      {
        "name": "Mechanical",
        "value": "Mechanical"
      },
      {
        "name": "Electrical",
        "value": "Electrical"
      }
    ],
    "default_value": null
  }
]

已判断:

  1. 使用不起作用的递归SquashNulls修改规范

  2. 删除等级库

  3. 现有堆栈溢出问题

推荐答案

您可以go 掉对#前缀的整数进行换行的方括号符号,同时递减除数组以外的那些整数(即"选项"),

例如.将100转换为101,将102转换为103;对于"options":105转换为106(keep square brackets),例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "createable": {
          "true": {
            "@(2,name)": ["&3.crm_field_identifier", "&3.target_property"],
            "@(2,nillable)": {
              "true": {
                "#false": "&5.required"
              },
              "false": {
                "#true": "&5.required"
              }
            },
            "@(2,picklistValues)": {
              "*": {
                "label": "&5.options[&1].name",
                "value": "&5.options[&1].value"
              }
            },
            "@(2,defaultValue)": "&3.default_value"
          }
        }
      }
    }
  },
  { // get rid of the integer object keys
    "operation": "shift",
    "spec": {
      "*": {
        "@": ""
      }
    }
  }
]

请记住,#-prefixed标识符从1开始计数,而&-prefixed0开始计数

现场http://jolt-demo.appspot.com/上的101个是:

enter image description here

Json相关问答推荐

将嵌套的json中的字符串强制转换为数字

JQ如何获取特定子元素的所有父母

如何创建生成两个不同对象的JSON数组的SQL查询?

Terraform迭代JSON文件以获取键值对

在AWS步骤函数中将字符串解析为JSON&S映射状态

将JSON数组组织到菜单中

在 VS Code 中将一个正则表达式替换为另一个正则表达式

将 JSON 文件放在哪里以在 Angular 8 应用程序中加载静态 JSON 数据?

在循环中将变量添加到 bash 数组

如何判断 Json 对象中是否存在键并获取其值

使用 JSONObject 在 Java 中为以下 struct 创建嵌套 JSON 对象?

如何创建 JSON 对象 Node.js

在视图中将 .Net 对象转换为 JSON 对象

PostgreSQL 中的 JSON 模式验证?

为什么 JavaScript 的 eval 需要括号来判断 JSON 数据?

js 中奇怪的 JSON 解析行为,Unexpected token :

在 .NET 中缩小缩进的 JSON 字符串

Backbone.js 模型与集合

从动态 json 数据更新力有向图上的链接

使用 JavaScriptSerializer() 反序列化 JSON 文件