0

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:

  1. Extract values in features
  2. Change data type in geometry to string
  3. Add slash before each comma in geometry string
2
  • 4
    BTW this is not forward slash... This one is -> / Commented Dec 10, 2023 at 14:33
  • 4
    I have a json file as below - where does this malformed JSON comes from in the first place? You should be fixing it there, at the source Commented Dec 10, 2023 at 14:59

1 Answer 1

1

As said in comment, the json you give can't used directly in Python (neither in a json file). Thus, I decide to use it as a Python dictionary.

import json
from pathlib import Path

dict_to_put_as_json = {
    "geometry": {
        "type": "LineString",
        "coordinates": [[25.4907, 35.29833], [25.49187, 35.28897]],
    }
}

geometry_as_string = json.dumps(dict_to_put_as_json["geometry"])
dict_to_write = {"geometry": geometry_as_string}

with Path("result.json").open(mode="w") as fp:
    json.dump(dict_to_write, fp)

Like this, it gives in the json file:

{
  "geometry": "{\"type\": \"LineString\", \"coordinates\": [[25.4907, 35.29833], [25.49187, 35.28897]]}"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.