Skip to main content
Fix grammar and reword title
Source Link
Kusalananda
  • 346k
  • 39
  • 711
  • 1k

Add Array of Object in Conditional JSON with conditiontransformation using jq

I have a system that outputoutputs multiple types of JSON filefiles with data.

Is there any chance to implement abovethe three conditions below using JQ packagejq? I am exploring the mapmap() command, and the arg--arg and argjson--argjson command. But line options, but I am having a tough time.

First JSON of, where I do have an assetasset object and I want to add following key value-value pair:

2nd JSON of, where I do notdon't have an assetasset object, so I want to add the object and the following key value-value pair

Desired output:Desired output:

3rd JSON of, where I do have an assetasset object, but I would like to change the key value-value pair of househouse if it hasis set to falsefalse and set colourcolour to orangeorange:

Desired output:Desired output:

Add Array of Object in JSON with condition

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

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

Desired output:

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

Desired output:

Conditional JSON transformation using jq

I have a system that outputs multiple types of JSON files with data.

Is there any chance to implement the three conditions below using jq? I am exploring the map() command, and the --arg and --argjson command line options, but I am having a tough time.

First JSON, where I have an asset object and I want to add following key-value pair:

2nd JSON, where I don't have an asset object, so I want to add the object and the following key-value pair

Desired output:

3rd JSON, where I have an asset object, but I would like to change the key-value pair of house if it is set to false and set colour to orange:

Desired output:

For 2nd scenario - my apologies i needed house set to true
Source Link

"house": falsetrue and "colour": "orange"

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

"house": false and "colour": "orange"

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

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

{
    "name": "Paul",
    "country": "USA",
    "spec": {
        ...
        "asset": {
            "house": true,
            "colour": "orange"
        },        
    }    
}
enhance
Source Link
mattia.b89
  • 3.3k
  • 4
  • 17
  • 45

Is there any possibilitychance to implement above three conditionconditions using JQ packageJQ package? I am exploring mapmap, argarg and argjsonargjson command. But I am having tough time

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

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

Source Link
Loading