Reputation: 23277
My jq
extract expression is getting too much large and I'm getting unconfortable using it:
jq -r '[.id,.meta.lastUpdated,.identifier[0].use, .identifier[0].system, .identifier[0].value, .identifier[1].use, .identifier[1].system, .identifier[1].value, .identifier[2].use, .identifier[2].system, .identifier[2].value, .identifier[2].assigner.reference, .active, .name[0].use, .name[0].text, .name[0].given[0], .name[0].family, .name[0]._family.extension[0].valueString, .address[0].extension[0].valueString, .address[0].type, (.address[0].line[]? | select(. | contains("TV^")) | split("^")[1]) // null, (.address[0].line[]? | select(. | contains("NV^")) | split("^")[1]) // null, (.address[0].line[]? | select(. | contains("NVI^")) | split("^")[1]) // null, .address[0].city, .address[0].state, .address[0].postalCode, .address[0].country, .qualification[0].code.coding[0].system, .qualification[0].code.coding[0].code] | @csv' practitioners-pre.json > practitioners-pre.csv
Is there any way tidy it up a bit?
Upvotes: 0
Views: 58
Reputation: 386541
First of all, you can add line breaks and remove . |
[
.id,
.meta.lastUpdated,
.identifier[0].use,
.identifier[0].system,
.identifier[0].value,
.identifier[1].use,
.identifier[1].system,
.identifier[1].value,
.identifier[2].use,
.identifier[2].system,
.identifier[2].value,
.identifier[2].assigner.reference,
.active,
.name[0].use,
.name[0].text,
.name[0].given[0],
.name[0].family,
.name[0]._family.extension[0].valueString,
.address[0].extension[0].valueString,
.address[0].type,
( .address[0].line[]? | select( contains( "TV^" ) ) | split( "^" )[1] ) // null,
( .address[0].line[]? | select( contains( "NV^" ) ) | split( "^" )[1] ) // null,
( .address[0].line[]? | select( contains( "NVI^" ) ) | split( "^" )[1] ) // null,
.address[0].city,
.address[0].state,
.address[0].postalCode,
.address[0].country,
.qualification[0].code.coding[0].system,
.qualification[0].code.coding[0].code
] | @csv
We can also move the address line searching logic into a function. With the code isolated, it's easier to make it better. In the process, I improved it to split first, and to handle multiple matching lines better. (You may need to adjust the select`.)
def addr_special_field($field):
[ .line[]? | split("^") | select( .[0] == $field ) ] | .[0][1]?;
[
.id,
.meta.lastUpdated,
.identifier[0].use,
.identifier[0].system,
.identifier[0].value,
.identifier[1].use,
.identifier[1].system,
.identifier[1].value,
.identifier[2].use,
.identifier[2].system,
.identifier[2].value,
.identifier[2].assigner.reference,
.active,
.name[0].use,
.name[0].text,
.name[0].given[0],
.name[0].family,
.name[0]._family.extension[0].valueString,
.address[0].extension[0].valueString,
.address[0].type,
( .address[0] | addr_special_field( "TV" ) ),
( .address[0] | addr_special_field( "NV" ) ),
( .address[0] | addr_special_field( "NVI" ) ),
.address[0].city,
.address[0].state,
.address[0].postalCode,
.address[0].country,
.qualification[0].code.coding[0].system,
.qualification[0].code.coding[0].code
] | @csv
This is long, but at least it's readable. Another thing we could do is factor out common terms, though I'm not sure it truly helps.
def addr_special_field($field):
[ .line[]? | split("^") | select( .[0] == $field ) ] | .[0][1]?;
[
.id,
.meta.lastUpdated,
( .identifier[0] | .use, .system, .value ),
( .identifier[1] | .use, .system, .value ),
( .identifier[2] | .use, .system, .value, .assigner.reference ),
.active,
( .name[0] | .use, .text, .given[0], .family, ._family.extension[0].valueString ),
( .address[0] |
.extension[0].valueString,
.type,
addr_special_field( "TV" ),
addr_special_field( "NV" ),
addr_special_field( "NVI" ),
.city,
.state,
.postalCode,
.country
),
( .qualification[0].code.coding[0] | .system, .code )
] | @csv
Upvotes: 3