I have two example files
example1:
{"label": "Example1","attributes": [ {"objectTypeAttribute": {"name": "Created"},"objectAttributeValues": [ {"value": "create time 1" } ] }, {"objectTypeAttribute": {"name": "Updated"},"objectAttributeValues": [ {"value": "update time 1" } ] } ]}and example2
{"label": "Example2","attributes": [ {"objectTypeAttribute": {"name": "Created"},"objectAttributeValues": [ {"value": "create time 2" } ] } ]}When I apply
cat example1 | jq '{created: .attributes[] | select(.objectTypeAttribute.name=="Created").objectAttributeValues[0].value,updated:.attributes[] | select(.objectTypeAttribute.name=="Updated").objectAttributeValues[0].value}'on the first everything works fine and I get expected result
{"created": "create time 1","updated": "update time 1"}, however applying
cat example2 | jq '{created: .attributes[] | select(.objectTypeAttribute.name=="Created").objectAttributeValues[0].value,updated:.attributes[] | select(.objectTypeAttribute.name=="Updated").objectAttributeValues[0].value}'I get no result at all, the result i would expect is
{"created": "create time 2","created": "NULL"}After researching similar questions I have tried the following:
cat example2 | jq 'try (.attributes[] | select(.objectTypeAttribute.name=="Created").objectAttributeValues[0].value) // "NULL", try (.attributes[] | select(.objectTypeAttribute.name=="Updated").objectAttributeValues[0].value) // "NULL"'however this doesn't work when I try rewriting the key like here:
cat example2 | jq '{created: (try (.attributes[] | select(.objectTypeAttribute.name=="Created").objectAttributeValues[0].value) // "NULL"), updated: try (.attributes[] | select(.objectTypeAttribute.name=="Updated").objectAttributeValues[0].value) // "NULL"}'Also I have tried
cat example2 | jq '{created: (.attributes[] | select(.objectTypeAttribute.name=="Created").objectAttributeValues[0].value? //"null1"),updated: (.attributes[] | select(.objectTypeAttribute.name=="Updated").objectAttributeValues[0].value? //"NULL")}'which works for example2 but doesn't for example1
cat example1 | jq '{created: (.attributes[] | select(.objectTypeAttribute.name=="Created").objectAttributeValues[0].value? //"NULL"),updated: (.attributes[] | select(.objectTypeAttribute.name=="Updated").objectAttributeValues[0].value? //"NULL")}'which results in
{"created": "create time 1","updated": "NULL"}{"created": "create time 1","updated": "update time 1"}{"created": "NULL","updated": "NULL"}{"created": "NULL","updated": "update time 1"}enter code here