0

I have a string below :-

d = '"{""Source"":""Test Flow Action""}"'

I need to convert it into a dict as :

d = {"Source":"Test Flow Action"}

I tried ast.literal_eval, json.loads, but was not able to succeed.

When I used ast.literal_eval, I am getting the O/P as below:

{Source:Test Flow Action}

But this is not in python.

Can anyone help me with this?

Thanks

2
  • Use the decoder for the encoder you used to create that string. Commented Apr 15, 2021 at 18:51
  • @Manuel Can you elaborate, please? Commented Apr 15, 2021 at 18:53

2 Answers 2

3
json.loads('"{""Source"":""Test Flow Action""}"'.replace('"{', '{').replace('}"', '}').replace('""', '"'))

Yields a dictionary looking like:

{'Source': 'Test Flow Action'}

which is python, indeed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This is Superrr!!
0

I was trying a little modified version of the same question just for knowledge purpose.

dt="{'a':1,'b':2}"
dict(dt.replace('}"','}').replace('{"','{'))

any way I can get dict type output {'a':1,'b':2}

1 Comment

I think I got the answer by using eval() like eval(dt)

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.