-3

When I extract some data from a data-source the data is getting extracted as a string object and the json is being exported as a string.

I have tried

json.loads() -> link1 Convert string to JSON in Python? and this convert a json string to python object from other answers.

However, the expectation is

"details": 
"{
'col1': 'col1_details',
'col2': 'col2_details',
'col3': 'col3_details'
}"

the expected outcome is

details":
{
'col1': 'col1_details',
'col3': 'col2_details',
'col3': 'col3_details'
}

as you see the extra quotes are causing the json to be read as a string and json.loads() and json.dumps() are not working.

I am open to any suggestions without using regex to create a json object directly/easily?

edit:

modified the string object - and the whole point as you see is that the data extraction process is creating a string instead of creating a json object bypassing an extra "" that makes it an invalid json object where I cannot use json.loads and json.dunps and hence the question.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Vyas
  • 57
  • 11
  • Does this answer your question? [Convert string to JSON in Python?](https://stackoverflow.com/questions/16577632/convert-string-to-json-in-python) – Maxwell D. Dorliea Jul 13 '22 at 17:59
  • 2
    That isn't valid JSON. Make a [mcve]. What is the *exact* content of the data object? – Mark Tolonen Jul 13 '22 at 18:00
  • No. It does not. I already tried that solution. – Vyas Jul 13 '22 at 18:01
  • 2
    @Vyas your expected output is not a valid JSON format – Maxwell D. Dorliea Jul 13 '22 at 18:04
  • Invalid JSON. Please first verify JSON with a validator. Also, this question just presents some output, without giving the **input**. Not really that handy... You also seem to have two different expectations. – trincot Jul 13 '22 at 18:04
  • You must provide a [mcve]. What, *exactly* are your inputs? "When I extract some data from a data-source the data is getting extracted as a string object and the json is being exported as a string." doesn't really make sense, it isn't clear what is going on precisely. Please take the time to create a properly specified question – juanpa.arrivillaga Jul 13 '22 at 18:09
  • So, you are dumping the string representation of a Python `dict` as the value of the `"details"` key (again, you **really** should provide a [mcve], still, currently, you do not have valid JSON anywhere). Then presumably you serialize that outer dict, and that string value is serialized as a JSON string. When you deserialize it, that value is a Python `str`. To get that back, you can use `eval` (or `ast.literal_eval` if you don't trust the source). But you **really** should fix whatever process is creating this – juanpa.arrivillaga Jul 13 '22 at 18:25

1 Answers1

1

Assuming you meant

"details": {
  'col1': 'col1_details',
  'col3': 'col2_details',
  'col3': 'col3_details'
}

Then re-loads the data

data["details"] = json.loads(data["details"])

But, this assumes whatever response you've gotten actually returns valid JSON, which single-quotes are not (there might be a good reason it is returned as a string instead)

Therefore, the most reasonable solution would be to modify the code that returned that response, not try to fix it via parsing it at a later point.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245