I am trying to parse a .json file into a .kml file to be used by a plotting program. I am going to give a set a data samples to simplify the issue:
I have a LocationHistory.json file that has the following structure:
{
 "data" : {
   "items" : [ {
     "kind" : "latitude#location",
     "timestampMs" : "1374870896803",
     "latitude" : 34.9482949,
     "longitude" : -85.3245474,
     "accuracy" : 2149
   }, {
     "kind" : "latitude#location",
     "timestampMs" : "1374870711762",
     "latitude" : 34.9857898,
     "longitude" : -85.3526902,
     "accuracy" : 2016"
   }]
  }
}
I define a function that parses the json data and then I want to feed it into a "placemark" string to write (output) into a "location.kml" file.
def parse_jason_data_to_kml_file():
   kml_file = open('location.kml', "r+")
   #Here I parse the info inside the LocationHistory.json file
   json_file = open('LocationHistory.json')
   json_string = json_file.read()
   json_data = json.loads(json_string)
   locations = json_data["data"]["items"]
   # Here I get the placemark string structure
   placemark = ["<Placemark>",
                "<TimeStamp><when>the “timestampMS” value from the JSON data item</when></TimeStamp>",
                "<ExtendedData>",
                "<Data name=”accuracy”>",
                "<value> the “accuracy” value from the JSON data item </value>]",
                "</Data>",
                "</ExtendedData><Point><coordinates>”longitude,latitude”</coordinates></Point>",
                "</Placemark>"]
   placemark = "\n".join(placemark)
   # Now i try to find certain substrings in the "placemark" string that I would like to replace:
   find_timestamp = placemark.find("the “timestampMS” value from the JSON data item")
   find_accuracy = placemark.find("the “accuracy” value from the JSON data item")
   find_longitude = placemark.find("”longitude")
   find_latitude = placemark.find("latitude”")
   # Next, I want to loop through the LocationHistory.json file (the data list above)
   # and replace the strings with the parsed LocationHistory.json data saved as: 
   # location["timestampMs"], location["accuracy"], location["longitude"], location["latitude"]: 
   for location in locations:                
       if find_timestamp != -1:
           placemark.replace('the “timestampMS” value from the JSON data item', location["timestampMs"])
       if find_accuracy != -1:
           placemark.replace('the “accuracy” value from the JSON data item', location["accuracy"])
       if find_longitude != -1:
           placemark.replace('”longitude', location["longitude"])
       if find_latitude != -1:
           placemark.replace('latitude”', location["latitude"])
       kml_file.write(placemark)
       kml_file.write("\n")
    kml_file.close()
The aim of this code is to write the placemark string that contains the .json data, into the location.ml file, from the placemark string that looks like this:
<Placemark>
<TimeStamp><when>the “timestampMS” value from the JSON data item</when></TimeStamp>
<ExtendedData>
<Data name=”accuracy”>
<value> the “accuracy” value from the JSON data item </value>
</Data>
</ExtendedData><Point><coordinates>”longitude,latitude”</coordinates></Point>
</Placemark>
To an output, which should look like this:
<Placemark>
<TimeStamp><when>2013-07-26T22:34:56Z</when></TimeStamp>
<ExtendedData>
<Data name=”accuracy”>
<value>2149</value>
</Data>
</ExtendedData><Point><coordinates>-85.3245474,34.9482949</coordinates></Point>
</Placemark>
<Placemark>
<TimeStamp><when>2013-07-26T22:31:51Z</when></TimeStamp>
<ExtendedData>
<Data name=”accuracy”>
<value>2016</value>
</Data>
</ExtendedData><Point><coordinates>-85.3526902,34.9857898</coordinates></Point>
</Placemark>
If I try to run this code, I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Elysium/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Users/Elysium/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/Elysium/Dropbox/LeeV/Task 5 - Location Plotting/ParseToKML.py", line 77, in <module>
parse_jason_data_to_kml_file()
File "/Users/Elysium/Dropbox/LeeV/Task 5 - Location Plotting/ParseToKML.py", line 51, in parse_jason_data_to_kml_file
placemark.replace('the “timestampMS” value from the JSON data item', location["timestampMs"])
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 33: ordinal not in range(128)
Is this a problem with my code or a software problem? From various sources I can find that it can be related to PIP or ascii or something, but no answer to help me... I'm sure I have pip installed...
Any help would be appreciated. Any suggestions to improve my code is also welcome :)
Thank You
 
    