Hello dear everyone,
one customer occasionally sends invalid JSON files for some reason, so I am trying to use KM in order to fix the missing data, then continue to calculate the geographic center of some given GPS coordinates.
An anonymised sample JSON file is here: https://www.splitmind.de/BrokenJSON.json.zip
First step in the macro to read the file, then determine the text encoding (usually it's iso-8859-1), then convert it with a shell script to UTF-8 so stuff works okay on the Mac when dealing with text and umlauts. Then, I want to fix the coordinates as follows:
There are two sets of coordinates submitted:
The first is the "Einzelpunkt" (single point) which the customer fails to provide in the sample, but a correctly formatted JSON file would expect a decimal number there, north and east coordinates. This is why the JSON is not properly formatted and can't be processed by Keyboard Maestro the way it is.
The second is "KoordinatenFlaeche", an array of GPS coordinates, north and east, to mark an area on the map. Those coordinates are always provided.
I plan to calculate the middle point of the given array coordinates and fill it into the Einzelpunkt coordinate. However, in order to read and calculate this correctly, Keyboard Maestro first has to parse it as properly formatted JSON.
At first I tried to do a simple Search & Replace. It does work with ""Nord" : ," which can only appear if the file is broken. However, ""Ost" : " has no comma at the end and appears twice in the data, so if I just want to replace it with ""Ost" : 8.0000", it will also do the same to the array, and then the JSON is just broken again!
So I thought I could use some RegEx to determine that I am changing the correct occurrence of "Ost", but that turned out harder than I thought.
Any ideas and input are very welcome!
--
JSON Repair Macro (v11.0.3)
JSON Repair.kmmacros (21 KB)