简体   繁体   English

如何用jq替换json文件中的值?

[英]how to replace a value in a json file with jq?

I have a json that has the following structure我有一个具有以下结构的 json

[{
    "model": "about.term",
    "pk": 20202,
    "fields": {
        "term": "Summer",
        "year": 2020
    }
},
{
    "model": "about.term",
    "pk": 20203,
    "fields": {
        "term": "Fall",
        "year": 2020
    }
},
{
    "model": "about.officer",
    "pk": 1,
    "fields": {
        "position": "President",
        "term_position_number": 0,
        "name": "Person 1",
        "start_date": "1984-01-01T00:00:00",
        "sfuid": "",
        "sfu_email_alias": "",
        "phone_number": 11111111111,
        "github_username": "",
        "gmail": "",
        "course1": "",
        "course2": "",
        "language1": "",
        "language2": "",
        "bio": "",
        "image": "about_static/exec-photos/stockPhoto.jpg",
        "elected_term": 19841,
        "sfu_officer_mailing_list_email": "NONE"
    }
},
{
    "model": "about.officer",
    "pk": 2,
    "fields": {
        "position": "Vice President",
        "term_position_number": 1,
        "name": "Person 2",
        "start_date": "1984-01-01T00:00:00",
        "sfuid": "",
        "sfu_email_alias": "",
        "phone_number": 2222222222222,
        "github_username": "",
        "gmail": "",
        "course1": "",
        "course2": "",
        "language1": "",
        "language2": "",
        "bio": "",
        "image": "about_static/exec-photos/stockPhoto.jpg",
        "elected_term": 19841,
        "sfu_officer_mailing_list_email": "NONE"
    }
},
{
    "model": "about.officer",
    "pk": 3,
    "fields": {
        "position": "Treasurer",
        "term_position_number": 2,
        "name": "Person 3",
        "start_date": "1984-01-01T00:00:00",
        "sfuid": "",
        "sfu_email_alias": "",
        "phone_number": 3333333333,
        "github_username": "",
        "gmail": "",
        "course1": "",
        "course2": "",
        "language1": "",
        "language2": "",
        "bio": "",
        "image": "about_static/exec-photos/stockPhoto.jpg",
        "elected_term": 19841,
        "sfu_officer_mailing_list_email": "NONE"
    }
},
{
    "model": "about.officer",
    "pk": 4,
    "fields": {
        "position": "Secretary",
        "term_position_number": 3,
        "name": "Person 4",
        "start_date": "1984-01-01T00:00:00",
        "sfuid": "",
        "sfu_email_alias": "",
        "phone_number": 444444444444,
        "github_username": "",
        "gmail": "",
        "course1": "",
        "course2": "",
        "language1": "",
        "language2": "",
        "bio": "",
        "image": "about_static/exec-photos/stockPhoto.jpg",
        "elected_term": 19841,
        "sfu_officer_mailing_list_email": "NONE"
    }
}]

how can I change it with bash jq command so that it has the exact same data, structure and indentation but the values for key sfuid become sfuid , sfu_email_alias become sfu_email_alias , phone_number become 0 , github_username become github_username and gmail become gmail .如何使用 bash jq命令更改它,使其具有完全相同的数据、结构和缩进,但键sfuid的值变为sfuidsfu_email_alias变为sfu_email_aliasphone_number变为0github_username变为github_usernamegmail变为gmail

basically, I want to end up with this file基本上,我想以这个文件结束

[{
    "model": "about.term",
    "pk": 20202,
    "fields": {
        "term": "Summer",
        "year": 2020
    }
},
{
    "model": "about.term",
    "pk": 20203,
    "fields": {
        "term": "Fall",
        "year": 2020
    }
},
{
    "model": "about.officer",
    "pk": 1,
    "fields": {
        "position": "President",
        "term_position_number": 0,
        "name": "Person 1",
        "start_date": "1984-01-01T00:00:00",
        "sfuid": "sfuid",
        "sfu_email_alias": "sfu_email_alias",
        "phone_number": 0,
        "github_username": "github_username",
        "gmail": "gmail",
        "course1": "",
        "course2": "",
        "language1": "",
        "language2": "",
        "bio": "",
        "image": "about_static/exec-photos/stockPhoto.jpg",
        "elected_term": 19841,
        "sfu_officer_mailing_list_email": "NONE"
    }
},
{
    "model": "about.officer",
    "pk": 2,
    "fields": {
        "position": "Vice President",
        "term_position_number": 1,
        "name": "Person 2",
        "start_date": "1984-01-01T00:00:00",
        "sfuid": "sfuid",
        "sfu_email_alias": "sfu_email_alias",
        "phone_number": 0,
        "github_username": "github_username",
        "gmail": "gmail",
        "course1": "",
        "course2": "",
        "language1": "",
        "language2": "",
        "bio": "",
        "image": "about_static/exec-photos/stockPhoto.jpg",
        "elected_term": 19841,
        "sfu_officer_mailing_list_email": "NONE"
    }
},
{
    "model": "about.officer",
    "pk": 3,
    "fields": {
        "position": "Treasurer",
        "term_position_number": 2,
        "name": "Person 3",
        "start_date": "1984-01-01T00:00:00",
        "sfuid": "sfuid",
        "sfu_email_alias": "sfu_email_alias",
        "phone_number": 0,
        "github_username": "github_username",
        "gmail": "gmail",
        "course1": "",
        "course2": "",
        "language1": "",
        "language2": "",
        "bio": "",
        "image": "about_static/exec-photos/stockPhoto.jpg",
        "elected_term": 19841,
        "sfu_officer_mailing_list_email": "NONE"
    }
},
{
    "model": "about.officer",
    "pk": 4,
    "fields": {
        "position": "Secretary",
        "term_position_number": 3,
        "name": "Person 4",
        "start_date": "1984-01-01T00:00:00",
        "sfuid": "sfuid",
        "sfu_email_alias": "sfu_email_alias",
        "phone_number": 0,
        "github_username": "github_username",
        "gmail": "gmail",
        "course1": "",
        "course2": "",
        "language1": "",
        "language2": "",
        "bio": "",
        "image": "about_static/exec-photos/stockPhoto.jpg",
        "elected_term": 19841,
        "sfu_officer_mailing_list_email": "NONE"
    }
}]

Assuming the input is valid JSON and that valid JSON is acceptable as output, the following jq program will perform the required transformations:假设输入是有效的 JSON 并且有效的 JSON 可接受作为输出,以下 jq 程序将执行所需的转换:

map( reduce ("sfuid", "sfu_email_alias", "github_username", "gmail") as $k (.;
         if .fields[$k] then .fields[$k] = $k else . end)
       | if .fields.phone_number then .fields.phone_number = 0 else . end )

Using reduce here is of course in support of "DRY".这里使用reduce当然是支持“DRY”。 If reduce is unfamiliar to you, you can think of it as a kind of for loop.如果您不熟悉reduce ,您可以将其视为一种for循环。

DRY-er烘干机

def update($PATH; VALUE):
  . as $in
  | if ($PATH | select(IN($in|paths)) // null) 
    then setpath($PATH;VALUE) else . end;

map( reduce ("sfuid", "sfu_email_alias", "github_username", "gmail") as $k (.;
         update( ["fields", $k]; $k )
       | update( ["fields", "phone_number"]; 0) ))


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM