I'm trying to access data from a file full of JSON objects, where the string fields may or may not have escaped quotes in them.
When I try to process it using while read line ; do echo ; done < input.txt
, it pukes because of the unbalanced number of quotes.
I have this input file:
$ cat input.txt
{"sku":"1234", "desc":"Necklace 18\" long", "img":"https://provider.com/12345.jpg", "imgView":"A"}
{"sku":"1234", "desc":"Necklace 18\" long", "img":"https://provider.com/12346.jpg", "imgView":"B"}
When I use read
to capture it on the way in, I lose the backslash.
$ while read line ; do echo "${line}" ; done < input.txt
{"sku":"1234", "desc":"Necklace 18" long", "img":"https://provider.com/12345.jpg", "imgView":"A"}
{"sku":"1234", "desc":"Necklace 18" long", "img":"https://provider.com/12346.jpg", "imgView":"B"}
$ while read line ; do echo "${line}" | sed 's/\\/\\\\/g' ; done < input.txt
{"sku":"1234", "desc":"Necklace 18" long", "img":"https://provider.com/12345.jpg", "imgView":"A"}
{"sku":"1234", "desc":"Necklace 18" long", "img":"https://provider.com/12346.jpg", "imgView":"B"}
I have this workaround that I'll use for now to unblock myself. But it's ugly and verbose.
#Showing that it's preserving the escape
$ input=input.txt ; counter=1; length=$(cat ${input} | wc -l)
$ while [ ${counter} -le ${length} ] ; do data=$(tail -n +${counter} ${input} | head -n 1 ) ; echo ${data} ; counter=$(( counter + 1)) ; done
{"sku":"1234", "desc":"Necklace 18\" long", "img":"https://provider.com/12345.jpg", "imgView":"A"}
{"sku":"1234", "desc":"Necklace 18\" long", "img":"https://provider.com/12346.jpg", "imgView":"B"}
#Showing that jq can actually process the data now
$ input=input.txt ; counter=1; length=$(cat ${input} | wc -l)
$ while [ ${counter} -le ${length} ] ; do desc=$(tail -n +${counter} ${input} | head -n 1 | jq '.desc' -r) ; echo ${desc} ; counter=$(( counter + 1)) ; done
Necklace 18" long
Necklace 18" long
I feel like I have to get way too low level into how the shell is handling the input. There has to be an easier way, or a flag or something that I'm missing.