I have some broken JSON files that I want to fix. The problem is that one of the fields, AcquisitionDateTime, is garbled:
{ "AcquisitionDateTime": 2016-04-28T17:09:39.515625, }
What I want to do is wrap the value in parentheses. I can do this with a regex:
perl -pi -e 's/\"AcqDateTime\": (.*),/\"AcqDateTime\": \"\1\",/g' t.json
Now I want to extend the regex so that if the JSON does not break, the content will not be wrapped in "" twice. The problem I am facing is that I do not know how to mix lookahead, if / then statements, and capture groups. Here is my attempt:
Lookahead, if you find a ", then capture what is between it. Else capture everything. perl -pi -e 's/\"AcqDateTime\": (?(?=\")\"(.*)\"|(.*)),/\"AcqDateTime:\" \"\1\",/g' t.json
This is the part I'm interested in fixing:
Lookahead for a \" -> if yes, then capture without it. \"(.*)\" Else capture all (.*) (?(?=\")\"(.*)\"|(.*)),
Will someone explain to me what I'm doing wrong?
Thanks in advance.
source share