V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Ryanjie
V2EX  ›  Bash

请教一下 shell 里使用 jq 处理 json 应该怎么写

  •  
  •   Ryanjie · Dec 12, 2021 · 2096 views
    This topic created in 1603 days ago, the information mentioned may be changed or developed.

    如题,有一个 json 文件,

    [
        {
            "id": 0001,
            "name": "Zhang.San_123456",
            "email": {
                "qqmail": "[email protected]",
                "163mail": "[email protected]"
            },
            "Address": {
                "Province": "Guangdong",
                "City": "Zhanjiang",
                "Country": "Xuwen"
            }
        },
        {
            "id": 0002,
            "name": "Li.Si_123456",
            "email": {
                "qqmail": "[email protected]",
                "163mail": "[email protected]"
            },
            "Address": {
                "Province": "Sichuan",
                "City": "Yibin",
                "Country": "Jiang'an"
            }
        },
        {
            "id": 0003,
            "name": "Wang.Wu_123456",
            "email": {
                "qqmail": "[email protected]",
                "163mail": "[email protected]"
            },
            "Address": {
                "Province": "Shanxi",
                "City": "Taiyuan",
                "Country": "Qingxu"
            }
        }
    ]
    
    

    我想获取里面 name 字段为 Li.Si_123456 的一部分,返回时移除 email 里的 163mail ,返回内容如下:

    [
        {
            "id": 0002,
            "name": "Li.Si_123456",
            "url": "http://xxx.com",
            "qqmail": "[email protected]",
            "Address": {
                "Province": "Sichuan",
                "City": "Yibin",
                "Country": "Jiang'an"
            }
        }
    ]
    

    请教一下这个使用 jq 处理应该怎么写呢,有大佬能指教一下吗?谢谢了^^

    Ryanjie
        2
    Ryanjie  
    OP
       Dec 13, 2021
    @gstqc 谢谢大佬指教👍
    Ryanjie
        3
    Ryanjie  
    OP
       Dec 13, 2021
    解决方法:
    1. 获取里面 name 字段为 Li.Si_123456 的一部分:`jq --arg NAME ${UserName} '.[] | select(.name==$NAME)'`
    2. 返回时移除 email 里的 163mail: `map({id, name, url, qqmail: .email.qqmail,...})`
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   799 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 21:49 · PVG 05:49 · LAX 14:49 · JFK 17:49
    ♥ Do have faith in what you're doing.