如何使用jq工具获取/更新数组中notebook_task的base_parameters值
使用jq获取和更新JSON中指定字段的值
目标JSON结构片段:
"settings": { "tasks": [ { "email_notifications": {}, "job_cluster_key":"artifactory_download_job_cluster", "max_retries":0, "min_retry_interval_millis":900000, "notebook_task": { "base_parameters": { "project":"app1", "path":"dir1/source1/file" } } } ] }
一、获取project和path的值
- 获取所有tasks中的
project值:jq -r '.settings.tasks[].notebook_task.base_parameters.project' your_file.json - 获取所有tasks中的
path值:jq -r '.settings.tasks[].notebook_task.base_parameters.path' your_file.json - 同时获取两个字段(以结构化形式输出):
jq '.settings.tasks[].notebook_task.base_parameters | {project, path}' your_file.json
二、更新project和path的值
更新单个字段
- 将
project更新为app2:jq '.settings.tasks[].notebook_task.base_parameters.project = "app2"' your_file.json > updated_file.json - 将
path更新为dir2/source2/new_file:jq '.settings.tasks[].notebook_task.base_parameters.path = "dir2/source2/new_file"' your_file.json > updated_file.json
同时更新两个字段
如果需要一次操作更新两个字段,且保留base_parameters中的其他字段(如有):
jq '.settings.tasks[].notebook_task.base_parameters += {project: "app2", path: "dir2/source2/new_file"}' your_file.json > updated_file.json
如果要完全替换base_parameters为仅包含这两个字段的对象:
jq '.settings.tasks[].notebook_task.base_parameters = {project: "app2", path: "dir2/source2/new_file"}' your_file.json > updated_file.json
内容的提问来源于stack exchange,提问作者Vaitheesh
相关产品推荐
相关产品推荐

