替换深度嵌入json文件中的特定键



我有一个本地生成的json文件(origin.json),我想替换这个origin中的一些键。并生成一个远程。这样我就可以按照它的端点有效负载格式将它发送到远程服务器。

我的起源。json是大的和深嵌入的,我可以迭代每个键和替换那些我需要的。但我想知道是否有一种高效且奇特的工具可以做到这一点?比如jq ?

下面是我嵌入的json
{
"timeoutMs": 3000,
"requestTopic": "local-cron",
"searchQuery": {
"checkin": "2023-01-10",
"checkout": "2023-01-11",
"numberRoomsNeeded": 0,
"adultsTotal": 2,
"childrenTotal": 0,
"currency": "EUR"
},
"requestContext": {
"userId": 666666666,
"userAuthLevel": 2,
"isUserstar": true,
"visitorCc1": "cn",
"trafficSourceId": 0,
"siteTypeId": 9,
"detectedUserType": "normal",
"travelPurpose": 2,
"affiliateId": 12345,
"languageCode": "en-us",
"currency": "CNY",
"siteType": 1,
"serverRole": "cron",
"action": "bp",
"visitorIdentifier": [
{
"type": "id-single",
"uvi": "00000000000000000000000"
},
{
"type": "user-identity",
"uvi": "66666666"
},
{
"type": "user",
"uvi": "77777777777"
}
],
"isInternal": true,
"enableExperiments": true,
"shouldTrackRequestInExperiments": true,
"starSettings": {
"isUserstar": true,
"isUserstarControlGroup": false,
"canStarUserSeeFreeBreakfast": true,
"canStarUserSeeFreeRoomUpgrade": true,
"starTier": 5,
"topstarBenefit": "",
"isRightstar": true,
"starDynamicPricing": {
"canSeestarDynamicPricingLevel3": true
},
"canStarUserSeeFreeCleaningFee": true,
"starVipSettings": [
{
"eligible": true,
"benefitName": "no_et",
"programType": "PriceMatchTrial",
"percentage": 0
}
]
},
"isCsRelocationMode": false,
"tripValueContext": {},
"visitorCountryRegion": "sh",
"paymentTiming": 1,
"includeConditional": false
},
"showDebug": false,
"hits": [
{
"hhhhid": 8228082,
"ppblock": {
"allMatchingBlocks": [
{
"blockId": 1,
"rawBlock": {
"occupancy": 2,
"price": 34425,
"roomId": 822808201,
"policygroupId": 346547507,
"mealplan": 2,
"channel": 581,
"currencyId": 2,
"maxPersons": 3,
"flags": 0,
"freeCancelUntil": 0,
"priceBase10Exponent": -2,
"packageId": 0,
"paymenttermsId": 38,
"vrFlags": 0,
"bundleId": 0
},
"blockStay": {
"stayNights": [
{
"polId": 346547507,
"rateId": 25728208,
"curId": 2,
"price": 344.25,
"price1": 0,
"channelId": 581,
"occupancy": 2,
"roomId": 822808201,
"initialPrice": 405,
"initialPrice1": 0
}
],
"stayNrRooms": 1,
"stayAvailableUntil": 1956105,
"stayPrice": 344.25,
"stayFlashDeal": 0,
"stayPromoTextId": 0,
"stayMinAdvanceRes": 1673388000,
"stayInventorySegmentId": 0,
"stayExperimentFlags": 0,
"stayRoomRateFlags": 4,
"stayIncludedProducts": 0
}
}
]
},
"selectedBlocks": [
"822808201_346547507_2_2_0"
],
"selected": {
"822808201_346547507_2_2_0": 1
}
}
],
"pipeline": 3
}

这里我标记了几个我想用'==>(新键)的

jq '.. | keys?' star-dragongate.json 
[
"hits",
"pipeline",
"requestContext",
"requestTopic",
"searchQuery",
"showDebug", ==> showdebug 
"timeoutMs"
]
[
"adultsTotal",
"checkin",
"checkout",
"childrenTotal",
"currency",
"numberRoomsNeeded"
]
[
"action",
"affiliateId", ==> Affilateid
"currency",
"detectedUserType",
"enableExperiments",
"starSettings",
"includeConditional",
"isCsRelocationMode",
"isInternal",
"isUserstar",
"languageCode",
"paymentTiming",
"serverRole",
"shouldTrackRequestInExperiments", ==> inexperiments
"siteType",
"siteTypeId",
"trafficSourceId",
"travelPurpose",
"tripValueContext",
"userAuthLevel",
"userId",
"visitorCc1",
"visitorCountryRegion",
"visitorIdentifier"
]
[
0,
1,
2
]
[
"type",
"uvi"
]
[
"type",
"uvi"
]
[
"type",
"uvi"
]
[
"canStarUserSeeFreeBreakfast",
"canStarUserSeeFreeCleaningFee",
"canStarUserSeeFreeRoomUpgrade",  ==> freeroom_upgrade
"starDynamicPricing",
"starTier",
"starVipSettings",
"isRightstar",
"isUserstar",
"isUserstarControlGroup",
"topstarBenefit"
]
[
"canSeestarDynamicPricingLevel3"
]
[
0
]
[
"benefitName",
"eligible",
"percentage",
"programType"
]
[]
[
0
]
[
"hhhhid",
"ppblock",
"selected",
"selectedBlocks"
]
[
"allMatchingBlocks"
]
[
0
]
[
"blockId",
"blockStay",
"rawBlock"
]
[
"bundleId",  ==> bundle_id
"channel",
"currencyId",
"flags",
"freeCancelUntil",
"maxPersons",
"mealplan",
"occupancy",
"packageId",
"paymenttermsId",
"policygroupId",
"price",
"priceBase10Exponent",
"roomId",
"vrFlags"
]
[
"stayAvailableUntil",
"stayExperimentFlags",
"stayFlashDeal",
"stayIncludedProducts",
"stayInventorySegmentId",
"stayMinAdvanceRes",
"stayNights",
"stayNrRooms",
"stayPrice",
"stayPromoTextId", ==> staypromotextid
"stayRoomRateFlags"
]
[
0
]
[
"channelId",
"curId",
"initialPrice",
"initialPrice1",
"occupancy",
"polId",
"price",
"price1",
"rateId",
"roomId"
]
[
0
]
[
"822808201_346547507_2_2_0"
]

我需要替换的键位于不同的嵌入层和块这个json。有什么建议可以有效地替换这些键吗?PS: json文件中的关键字是在静态位置,而不是动态变化的所有时间。

要重命名字段名,可以使用with_entries,这样可以访问每个.key。通过赋值重置。

现在,还不清楚的是如何找到有问题的键(编程)。如果它们的位置是静态的,并且您知道它们,则分别对它们进行地址处理,如:

.requestContext.starSettings |= with_entries((.key | select(. == "canStarUserSeeFreeRoomUpgrade")) = "freeroom_upgrade")
| .hits[].ppblock.allMatchingBlocks[].rawBlock |= with_entries((.key | select(. == "bundleId")) = "bundle_id")
# and so on...

为了减少冗余代码,您还可以将它的重命名部分移动到它自己的函数中,然后只调用它,例如:

def rename($old; $new):
with_entries((.key | select(. == $old)) = $new);
.requestContext.starSettings |= rename("canStarUserSeeFreeRoomUpgrade"; "freeroom_upgrade")
| .hits[].ppblock.allMatchingBlocks[].rawBlock |= rename("bundleId";  "bundle_id")
# and so on...

或者将位置也移动到函数中,例如:

def rename_at(path; $old; $new):
path |= with_entries((.key | select(. == $old)) = $new);
rename_at(.requestContext.starSettings; "canStarUserSeeFreeRoomUpgrade"; "freeroom_upgrade")
| rename_at(.hits[].ppblock.allMatchingBlocks[].rawBlock; "bundleId";  "bundle_id")
# and so on...

如果它们的位置未知,并且您希望仅根据它们的(本地)名称替换它们,则需要遍历文档,并检查是否遇到匹配的名称。walk函数为您提供遍历,objects减少对对象的操作:

walk(objects |= with_entries(
if .key == "canStarUserSeeFreeRoomUpgrade" then .key = "freeroom_upgrade"
elif .key == "bundleId" then .key = "bundle_id"
# and so on...
else . end
))

相关内容

  • 没有找到相关文章

最新更新