Modify object properties conditionally with jq
<p>I have this sample of JSON:</p>

<pre><code>[
    {
        "name": "val1",
        "expire": { "$value": 10 }
    },
    {
        "name": "val2",
        "expire": 20
    },
    {
        "name": "val3"
    }
]
</code></pre>

<p>And I want to transform it to this form with jq:</p>

<pre><code>[
    {
        "name": "val1",
        "expire": 10
    },
    {
        "name": "val2",
        "expire": 20
    },
    {
        "name": "val3",
        "expire": null
    }
]
</code></pre>

<p>All that I've found it's if-then-else, but it looks like I have no clue how to build right expression.</p>

<p>Condition based on type check, it looks like a right way, but just return "compile"-time error, I don't know how to fix it:</p>

<pre><code>.[] | { name, expire: (if .expire then (if type(.expire) == "number" then .expire else .expire."$value" end) else null end) }
</code></pre>

<p>Condition based on <code>"$value"</code> check, somehow filter out second object:</p>

<pre><code>.[] | { name, expire: (if .expire then (if .expire."$value"? then .expire."$value" else .expire end) else null end) }
</code></pre>

<p>As I understand, the problem here in internal if, where second object checked with <code>.expire."$value"?</code>, error was thrown and object removed from result because of error.</p>