jq retain missing array objects from capturing group and update array

前端 未结 3 2060
后悔当初
后悔当初 2021-01-29 03:47

Input

[{
        \"tags\": [{
                \"value\": \"domain:sourcing\"
            },
            {
                  


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-29 04:35

    Here's another approach. This assumes your tags array will only contain the names you expect.

    map(
        reduce (.tags[].value | split(":")) as [$k,$v] (
            {domain:"-",apiname:"-"};
            .[$k] = $v
        )
    )
    

    For a more general solution that doesn't assume fixed names and just flattens the tags, I'd do this:

    map(
        reduce (.tags[].value | split(":")) as [$k,$v] (
            del(.tags);
            .[$k] = $v
        )
    )
    

    Then as you access the fields, just use the alternative operator to set the default value.

    (.domain // "-") as $domain
    

提交回复
热议问题