jsonjqjsonnet

JSON Query - 'jq' how do I get / update an object from an array or list


I have the below code in which I need to get/update the values of an object. I am able to get jq -r .settings.tasks What I need is to get and update values of base_parameters under notebook_task and it's objects project/path using jq utility.

"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


Solution

  • To access notebook_task - since tasks is an array you can use array[] syntax:

    jq '.settings.tasks[].notebook_task' yourfile.json
    

    or

    jq '.settings.tasks[].notebook_task.base_parameters' yourfile.json
    

    if you want to update a value inside it's the same idea

    jq '.settings.tasks[].notebook_task.base_parameters.project = "app2"' yourfile.json