skip to Main Content

I have system that output multiple types of JSON file with data

Is there any chance to implement above three conditions using JQ package? I am exploring map, arg and argjson command. But I am having tough time

First JSON of where I do have asset object and I want to add following key value pair

"house": true and "colour": "orange"

{
    "name": "Paul",
    "country": "USA",
    "spec": {
        ...
        "asset": {
            "yard": true
        },        
    }    
}

Desired output:

{
    "name": "Paul",
    "country": "USA",
    "spec": {
        ...
        "asset": {
            "house": true,
            "colour": "orange",
            "yard": true
        },        
    }    
}

2nd JSON of where I do not have asset object so I want to add the object and following key value pair

"house": true and "colour": "orange"

{
    "name": "Paul",
    "country": "USA",
    "spec": {
        ...
    }    
}

Desired output:

{
    "name": "Paul",
    "country": "USA",
    "spec": {
        ...
        "asset": {
            "house": true,
            "colour": "orange"
        },        
    }    
}

3rd JSON of where I do have asset object but I would like to change key value pair of house if it has set to false and set colour to orange

{
    "name": "Paul",
    "country": "USA",
    "spec": {
        ...
        "asset": {
            "house": false,
            "colour": "black"
        },        
    }    
}

Desired output:

{
    "name": "Paul",
    "country": "USA",
    "spec": {
        ...
        "asset": {
            "house": true,
            "colour": "orange"
        },        
    }    
}

Basically what I am trying to do is – If there is no asset object -> Create asset object, put house set to true and colour ornage in the element. If there is asset object already -> check if house is set to something else AND check colour is SET to something else, if that is the case then set hosue back to true and colour to orange. If there is asset class already in place, but house and colour key is not present in the object, then add house to true and colour to orange

2

Answers


  1. Lets assume you are storing the key-value pairs in the variable which you will pass as json argument to your query like below,

    input='{"house":true,"colour":"orange"}'
    

    Now if you use the above input in the following query all of your scenarios will work out,

    jq --argjson input $input '.spec.asset+= $input' <original_file>.json
    
    Login or Signup to reply.
  2. So you always want to have an asset with house=true and colour=orange in your output, potentially overwriting any existing objects or values?

    If that’s the case, then simply add a nested asset object with the desired properties to your objects. There is no logic involved:

    jq '.spec.asset = {house:true, colour:"orange"}'
    

    Input:

    {
      "name": "Paul",
      "country": "USA",
      "spec": {
        "asset": {
          "yard": true
        }
      }
    }
    {
      "name": "Paul",
      "country": "USA",
      "spec": {}
    }
    {
      "name": "Paul",
      "country": "USA",
      "spec": {
        "asset": {
          "house": false,
          "colour": "black"
        }
      }
    }
    

    Output:

    {
      "name": "Paul",
      "country": "USA",
      "spec": {
        "asset": {
          "house": true,
          "colour": "orange"
        }
      }
    }
    {
      "name": "Paul",
      "country": "USA",
      "spec": {
        "asset": {
          "house": true,
          "colour": "orange"
        }
      }
    }
    {
      "name": "Paul",
      "country": "USA",
      "spec": {
        "asset": {
          "house": true,
          "colour": "orange"
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search