首页 > 解决方案 > 根据名称转换 JSON 的输出

问题描述

输入是从卡中提取的数据。发现很难提取,尤其是因为0.jpg是根据图像名称动态生成的

.doc.0.jpg 变得无效,因此无法获取 0.jpg 以下的值

"doc": {
    "0.jpg": {
      "cNumber": "6218961 450 3875",
      "cNames": " 1 VEN NUT 2 SIRKA SARTH",
      "cExpiry": "09/2025"
    },
    "number": {
      "nTotal": 3,
      "nRequires": 3,
      "nDocuments": 1
    }
  }

我应该能够根据 cNames 中的数字提取名称

输出应该是

"doc": {
    "0.jpg": {
      "cNumber": "6218961 450 3875",
      "cNames": [ "VEN NUT",
                  "SIRKA SARTH"],
      "cExpiry": "09/2025"
    },
    "number": {
      "nTotal": 3,
      "nRequires": 3,
      "nDocuments": 1
    }
  }

标签: jqueryregexjq

解决方案


.doc |= map_values(if has("cNames")
                   then .cNames |= [splits(" *[0-9]+ *")][1:]
                   else . end)

推荐阅读