I have a json file as below, in which the value of geometry is a string.
I need to add a single slash in front of each double quote inside the string
from:
{"geometry":
"{"type": "LineString", "coordinates": [[25.4907, 35.29833],[25.49187, 35.28897]]}"
}
to:
{"geometry":
"{\"type\": \"LineString\", \"coordinates\": [[25.4907, 35.29833], [25.49187, 35.28897]]}"}
I tried to achieved this by using replace before exporting by json library
obj['geometry'] = str(feat['geometry']).replace("'","\\'")
with open(json_filepath, 'w') as f:
f.write('\n'.join(map(json.dumps, data)))
but the output is double slash:
"geometry": "{\\"type\\": \\"MultiPolygon\\", \\"coordinates\\":
...
How to generate a file with one slash only, thanks~
---- edited
Adding example of source data:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[
121.51749976660096,
25.04609631049641
],
[
121.51870845722954,
25.045781689873138
],
[
121.51913536000893,
25.045696164346566
]
]
},
"properties": {
"model": {
"RoadClass": "3",
"RoadClassName": "省道一般道路",
"RoadID": "300010",
"RoadName": "臺1線",
"RoadNameID": "10",
"InfoDate": "2015-04-01T00:00:00"
}
}
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[
121.51913536000893,
25.045696164346566
],
[
121.51938079578713,
25.045646605406546
],
[
121.51946871710766,
25.0456288491921
]
]
},
"properties": {
"model": {
"RoadClass": "3",
"RoadClassName": "省道一般道路",
"RoadID": "300010",
"RoadName": "臺1線",
"RoadNameID": "10",
"InfoDate": "2015-04-01T00:00:00"
}
}
}
]
}
The original data is in geojson format from an API. what I'm going to do for this data is to insert it into Bigquery in a Geography data type, with reference from here.
According to the blog, I need to:
- Extract values in
features - Change data type in
geometryto string - Add slash before each comma in
geometrystring
/