我有一个样本json数据,如:
{
"phone_number": "780-414-2085",
"city": "Edmonton",
"updated": "2015-10-19T00:03:10",
"name": "Sir William Place ",
"url": "http://www.bwalk.com/en-CA/Rent/Details/Alberta/Edmonton/Sir-William-Place",
"last_visited": "2015-10-19T00:03:10",
"rooms": [{
"available": "Available",
"bathrooms": ["1"],
"suite_type": "1 Bedroom",
"square_feet": ["594", "649"],
"deposit": ["$499"],
"price_range": ["$1059", "$1209"]
}, {
"available": "Available",
"bathrooms": ["1"],
"suite_type": "1 Bedroom + Den",
"square_feet": ["771"],
"deposit": ["$499"],
"price_range": ["$1169", "$1249"]
}, {
"available": "Available",
"bathrooms": ["1", "2"],
"suite_type": "2 Bedroom",
"square_feet": ["894", "970"],
"deposit": ["$499"],
"price_range": ["$1344", "$1494"]
}, {
"available": "Available",
"bathrooms": ["2"],
"deal": ["October FREE and $299 Security Deposit on 12 month leases "],
"suite_type": "2 Bedroom Bi-level",
"square_feet": ["894"],
"deposit": ["$499"],
"price_range": ["$1344", "$1394"]
}, {
"available": "Waiting List",
"bathrooms": ["1"],
"suite_type": "Bachelor",
"square_feet": ["540"],
"deposit": ["$499"],
"price_range": ["$1004", "$1054"]
}],
"address": "8830-85 St., Edmonton, Alberta, T6C 3C3",
"zip_code": "T6C 3C3"
}
我正在运行一个jq表达式,如:
'{phone_number, city, updated, name, address, zip_code, url, last_visited} + (.rooms[] | {suite_type, price_range_start: .price_range[0], price_range_end: .price_range[1]} + {available, square_foot_start:.square_feet[0], square_foot_end:.square_feet[1], deposit:.deposit[0], bathrooms:.bathrooms[0]})'
这给了我一个ok输出但重复相同的名称,因为我只列出了rooms数组。我希望能够将房间数组中的每个项目设置为类似room1,room2,room3等的东西。但是也要将它保存在一个条目中,所以例如这里的示例最终有5个条目,因为有5个房间和例如,名称重复5次,因为我现在设置它的方式。我想我需要将房间映射到某些东西,但不知道该怎么做。
有人可以就如何做到这一点提出建议吗?
答案 0 :(得分:0)
您可以更新数组中的元素,同时保留其他元素,如下所示:
'.rooms[] |= {suite_type, price_range_start: .price_range[0],
price_range_end: .price_range[1]} + {available,
square_foot_start:.square_feet[0], square_foot_end:.square_feet[1],
deposit:.deposit[0], bathrooms:.bathrooms[0]}'
答案 1 :(得分:0)
这是一个使用函数的解决方案。
def common_columns:
"phone_number", "city", "updated", "name", "address", "zip_code", "url", "last_visited"
;
def common:
.phone_number, .city, .updated, .name, .address, .zip_code, .url, .last_visited
;
def room_columns(n):
range(n)
| (
"available_\(.)", "bathrooms_\(.)", "suite_type_\(.)",
"square_feet_start_\(.)", "square_feet_end_\(.)", "deposit_\(.)",
"price_range_start_\(.)", "price_range_end_\(.)"
)
;
def rooms(n):
. as $r
| range(n)
| $r.rooms[.]
| (
.available, .bathrooms[0], .suite_type,
.square_feet[0,1], .deposit[0], .price_range[0,1]
)
;
[ common_columns, room_columns(6) ]
, [ common, rooms(6) ]
| @csv
您可以将6
更改为您需要的多组房间列。