I am hoping I can get an answer on this question. I am using ./jq to convert a CSV file into a file of JSON objects. Each line of the CSV input represents a JSON object. A sample of the CSV input is as follows:
loyalty_card_num,extended_data_source,is_pharmacy_linked,first_linked_date,linked_store,is_caregiver,caregiver_first_linked_date,caregiver_store
6105700000000170419,PharmacyLinks,false,,,false,,
6105700000006125318,PharmacyLinks,True,2022-12-27,7236,false,,
The resulting JSON file I am trying to create is:
{"loyaltyID":"6105700000000170419","extendedDataSource":"PharmacyLinks","extendedData":{"firstLinkDate":"","isCaregiver":"false","caregiverStores":"","caregiverFirstLinkDate":"","isPharmacyLinked":"false","linkedStores":""}}
{"loyaltyID":"6105700000006125318","extendedDataSource":"PharmacyLinks","extendedData":{"firstLinkDate":"2022-12-27T00:00:00-0500","isCaregiver":"false","caregiverStores":"","caregiverFirstLinkDate":"","isPharmacyLinked":"True","linkedStores":"7236"}}
My ./jq code is:
jq --slurp --raw-input --raw-output \
'split("\n") | .[1:] | map(split(",")) |
map(
{
"loyaltyID": .[0],
"extendedDataSource": .[1],
"extendedData":
{
"firstLinkDate": .[3],
"isCaregiver": .[5],
"caregiverStores": .[7],
"caregiverFirstLinkDate": .[6],
"isPharmacyLinked": .[2],
"linkedStores": .[4]
}
}
)' \
short.csv > short.json
The result of the above code creates an array of nested JSON object:
[
{
"loyaltyID": "6105700000000170419",
"extendedDataSource": "PharmacyLinks",
"extendedData": {
"firstLinkDate": "",
"isCaregiver": "false",
"caregiverStores": "",
"caregiverFirstLinkDate": "",
"isPharmacyLinked": "false",
"linkedStores": ""
}
},
{
"loyaltyID": "6105700000006125318",
"extendedDataSource": "PharmacyLinks",
"extendedData": {
"firstLinkDate": "2022-12-27",
"isCaregiver": "false",
"caregiverStores": "",
"caregiverFirstLinkDate": "",
"isPharmacyLinked": "True",
"linkedStores": "7236"
}
}
]
I don't want an array but a separate JSON object for each elements in the array. Using the -c switch will just put the entire output on one line. So my goal is to get rid of the [] and have one object per line. Thank you in advance.