1

I am trying to convert this string into a json object : -

coordinates = "{ lat: '55,7303650017903', lng: '12,3446636123658' }"

I tried using json.loads(), it didn't work for me. Is there a way I can achieve this?

asked Mar 9, 2020 at 11:01
4
  • string looks like javascript object Commented Mar 9, 2020 at 11:05
  • I was just about to comment as per @Chris Doyles comment -- this is not a python dictionary as your keys do not have quotes around them Commented Mar 9, 2020 at 11:05
  • 1
    The problem you have is that this string is neither a valid python dict nor valid json object. Commented Mar 9, 2020 at 11:06
  • Okay. Thanks for the correction. But is there a way I can retrieve the value of a key? For example , the value of 'lat' Commented Mar 9, 2020 at 11:09

4 Answers 4

3

I recommend using demjson python parser for achieving this , since the string is js object

import demjson
print(demjson.decode("{ lat: '55,7303650017903', lng: '12,3446636123658' }"))
answered Mar 9, 2020 at 11:11
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. This is really useful
Note that other solutions do not need installing package
2

Using regex to add quotes around the key and then use the ast module

Ex:

import re
import ast
coordinates = "{ lat: '55,7303650017903', lng: '12,3446636123658' }"
coordinates = re.sub(r"(\w+)(?=:)", r'"1円"', coordinates)
print(ast.literal_eval(coordinates))

Output:

{'lat': '55,7303650017903', 'lng': '12,3446636123658'}
answered Mar 9, 2020 at 11:14

Comments

1

A possible solution would be to use a regular expression:

import re
coordinates = "{ lat: '55,7303650017903', lng: '12,3446636123658' }"
pattern = r"{ lat: '(.*)', lng: '(.*)' }"
lat, lng = re.findall(pattern, coordinates)[0]

This solution does not require importing any additional packages beyond re. If you want then want a dictionary with those values:

cords = { "lat": lat, "lng": lng }
answered Mar 9, 2020 at 11:14

Comments

-1

You need to use json.loads:

import json
coordinates = '{ "lat": "55,7303650017903", "lng": "12,3446636123658" }'
datastore = json.loads(coordinates)
print(json.dumps(datastore))

You can try in an online repl: https://repl.it/languages/python3

answered Mar 9, 2020 at 11:12

4 Comments

This isnt a solution since your string is not the same as the string the OP has
Sure, like its been said need quotes on the keys. this is implicit.
yeah but this isnt a general solution. you have to assume the OP is getting this string from somewhere else and it could haev anything in it. If your answer is the OP should just implicitly change the string. you may as well say the OP can implicitly write it as s dict in the code
Ok, Chris! Got it! Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.