代码之家  ›  专栏  ›  技术社区  ›  Mina

使用RegEx从JSON中移除对象

  •  0
  • Mina  · 技术社区  · 7 年前

     {
         "1f626": {
             "name": "frowning face with open mouth",
             "ascii": [],
             "code_points": {
                 "base": "1f626",
                 "default_matches": [
                     "1f626"
                 ],
                 "greedy_matches": [
                     "1f626"
                 ],
                 "decimal": ""
             }
         }
     }
    

    我得把 code_points 对象使用 正则表达式 .


    我试过用这个正则表达式:

    (("code\w+)(.*)(}))
    

    但它只是选择了第一行。 代码点 反对。

    我该怎么做?


    注意:我必须使用正则表达式而不是JavaScript来删除它。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Tom Blodget    7 年前

    或者,在命令行中,如果可以使用 jq

    jq "del(.[].code_points)" <monster.json >smaller_monster.json
    

    code_points 每个二级对象内的键。

    我的机器花了5秒钟处理一份60MB的文件。

    它不是正则表达式,但也不是JavaScript。所以,它满足了一半的非功能性需求。

        2
  •  1
  •   CrafterKolyan    7 年前

    ("code_points")([\s\S]*?)(})

    . 实际上是任何角色除了 \n [\s\S] * 通过添加 ?

    记住,如果有内部对象(其他 {} )在 code_points