I have following json:
{
"Detail": {
"Response": [
{
"ID": "8000000D-1483989576",
"Name": "",
"FullName": "FullName 1"
},
{
"ID": "8000000C-1483985849",
"Name": "Name 1"
},
{
"ID": "80000006-1481277410",
"Name": "Name 2",
"FullName": "FullName 2"
},
{
"ID": "8000000B-1481537384",
"Name": "Name 3"
}
]
}
}
I'm trying to create another json that will consider the non-empty/not null .Name
as priority otherwise get .FullName
regardless if it's empty or null, the final json would look like following:
[
{
"id": "8000000D-1483989576",
"name": "FullName 1"
},
{
"id": "8000000C-1483985849",
"name": "Name 1"
},
{
"id": "80000006-1481277410",
"name": "FullName 2"
},
{
"id": "8000000B-1481537384",
"name": "Name 3"
}
]
The temporary solution I got is to use join
jq '[.Detail.Response[] | {id: .ID, name: [.Name, .FullName] | join("") }]'
But of course, it'll only work if .FullName
is empty or null.
This should get you on your way:
.Detail.Response[]
| { id: .ID, Name: (if .Name != "" then .Name else .FullName end) }
我找到了一种方法来使用map
和select
来做到这一点。
jq '[.Detail.Response[] | {id: .ID, name: [.Name, .FullName] | map(select(length > 0)) | first }]'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.