3

I am trying to remove quotes for all the elements present in data["clonedRadarsdetailslist"] shown below.

  1. Quotes dont get removed

  2. Not all the elements are present after the script is executed probably because I am overwriting

import datetime
data = {
    'orgRadar': u'37125110',
    'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953),
    'clonedRadarsdetailslist': [
        "{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}",
        "{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}",
    ],
}
print data
for radardetails in data['clonedRadarsdetailslist']:
    radardetails = radardetails.strip('\"')
    data['clonedRadarsdetailslist'] = radardetails
print data

CURRENT RESULT:

{'orgRadar': u'37125110', 'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953), 'clonedRadarsdetailslist': "{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}"}

EXPECTED RESULT:

{'orgRadar': u'37125110', 'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953), 'clonedRadarsdetailslist': [{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}, {'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}]}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Jeremyapple
  • 253
  • 2
  • 6
  • 20

3 Answers3

4

The error you receive tells you that single quotes are not valid. You may replace them with double quotes before attempting to load:

>>> [json.loads(x.replace("'", '"')) for x in data['clonedRadarsdetailslist']]

[{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'},
 {'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}]

Also, it's a bad idea to mutate the container that you're looping over. Don't do that!


EDIT: A "full solution", as requested.

import datetime
import json

data = {
  'orgRadar': u'37125110',
  'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953),
  'clonedRadarsdetailslist': [
    "{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}",
    "{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}"
    ]}

data['clonedRadarsdetailslist'] = [json.loads(x.replace("'", '"'))
  for x in data['clonedRadarsdetailslist']]

print(data)

Run online

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • can you explain what `mutate the container` means? – Jeremyapple May 10 '18 at 23:57
  • 1
    The json format is somewhat strict on what it will accept, for details see http://json.org/. This solution will break if the values contain quote characters but look like this shouldn't be a problem. – Penguin Brian May 10 '18 at 23:59
  • Try these: [1](https://unspecified.wordpress.com/2009/02/12/thou-shalt-not-modify-a-list-during-iteration/), [2](https://stackoverflow.com/questions/1637807/modifying-list-while-iterating) – Mateen Ulhaq May 10 '18 at 23:59
  • 2
    Good solution. It looks like python code and it deserializes with python's `ast.literal_eval()`. JSON and python literals look similar but if it is indeed python, the JSON trick will fail sometimes and work others. For instance, what if its a unicode `u"foo"` string? – tdelaney May 11 '18 at 00:00
  • @MateenUlhaq - your solution still didn't remove the quotes – Jeremyapple May 11 '18 at 00:02
  • @Jeremyapple I ran this in the REPL to produce the output you see above. You must be doing something incorrectly. – Mateen Ulhaq May 11 '18 at 00:03
  • The code in the question isn't actually mutating the list, but is reassigning the value to something else. I think this should be OK as the original list is still preserved and used by the for loop, but you would only get the last value of the list - which is probably unexpected. – Penguin Brian May 11 '18 at 00:05
  • can you share your full solution?are you getting the EXPECTED RESULT shared in my question – Jeremyapple May 11 '18 at 00:05
  • I like the improved formatting of the data structure. Much easier to understand now. – Penguin Brian May 11 '18 at 00:11
  • I think my solution (one in my 2nd edit), should output an array of information without the quotes. I don't know if you were expecting all the information to be outputted into one single array, but this outputs several arrays containing the paired information. – Cillian Collins May 11 '18 at 00:25
2

You have a key:value JSON 'dictionary' which needs to be converted to a series of arrays in order to loop through the values. You need to use the JSON.loads() function to accomplish this. I'm not 100% sure what values you want, nor did I change your code to suit any specific values other than what was in the other code as I'm not sure what you want, but this should convert the JSON data to a superarray which can then be looped through:

import datetime
data = {'orgRadar': u'37125110', 'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953), 'clonedRadarsdetailslist': ["{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}", "{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}"]}
json = json.loads(data)
for radardetails in json['clonedRadarsdetailslist']:
    radardetails = radardetails.strip('\"')
    data['clonedRadarsdetailslist'] = radardetails

print json

Should work, not sure what you're actually looking to get from it but you need to convert the JSON to a series of arrays and then loop through them to get the necessary values.

EDIT: Really sorry for jumping the gun and assuming I knew what was happening. You have a dictionary which is not what I initially thought you had. Here is a solution which will give you several arrays of information, you can pull whatever information you'd like from here:

import datetime
data = {'orgRadar': u'37125110', 'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953), 'clonedRadarsdetailslist': ["{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}", "{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}"]}
list = []
for key, value in data.items():
    temp = [key,value]
    list.append(temp)

for item in list:
    print (item)

EDIT 2: Just refined the code to better suit your needs, it will now (I hope) serve the function you wanted:

import datetime
import ast
data = {'orgRadar': u'37125110', 'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953), 'clonedRadarsdetailslist': ["{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}", "{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}"]}
list = []
radarlist = []
for key, value in data.items():
    temp = [key,value]
    list.append(temp)

radar = ast.literal_eval(list[2][1][0])

for key, value in radar.items():
    temp = [key,value]
    radarlist.append(temp)

for item in radarlist:
    print(item)
Cillian Collins
  • 728
  • 3
  • 11
  • 2
    I am guessing that the user doesn't actually want to strip any quotes (so that line is not required), but is getting confused with the appearance of the quote characters output by print. The data he supplied does not contain any double quote characters. – Penguin Brian May 10 '18 at 23:44
  • @Cillian - your code throws an error `TypeError: expected string or buffer` ,did it work for you? – Jeremyapple May 10 '18 at 23:46
  • @PenguinBrian - No ,there re double quite character,each element in the `data[clonedRadarsdetailslist]` has quotes around it.. – Jeremyapple May 10 '18 at 23:47
  • This solution is not correct. loads takes a string and converts it to python data, but we already have the python data. It is only the clonedRadarsdetailslist field that needs this treatment (for item in the list). – Penguin Brian May 10 '18 at 23:47
  • 1
    Python prints the string with double quotes around it because it is a string. The string itself doesn't contain the double quotes. – Penguin Brian May 10 '18 at 23:49
  • Give me a minute to try and fix the error. I was only pointing out that he needs convert it from JSON to an array, I know I left some of his original code in the solution so that's likely the error but I'll try to tidy my answer up. – Cillian Collins May 10 '18 at 23:50
  • @PenguinBrian - Thats not what I am looking for thought,My ask is simple ,to remove the quotes for each element in `data[clonedRadarsdetailslist]` – Jeremyapple May 10 '18 at 23:51
  • JSON is a serialization format. `data` is clearly already a python dictionary. – tdelaney May 10 '18 at 23:51
  • By the way, you will need to include json module. – Cillian Collins May 10 '18 at 23:53
  • Sorry for the confusion. Posted some code which **SHOULD** work. It runs fine, just not 100% sure if the output is going to be exactly what you need, but it does return an array with all the info I think you wanted. – Cillian Collins May 11 '18 at 00:05
  • It is a dictionary containing an array of "json like" strings that aren't actually json complaint. – Penguin Brian May 11 '18 at 00:10
  • I fixed it up to loop through, and convert, the dictionary which was inside the dictionary. Should give the information needed? – Cillian Collins May 11 '18 at 00:22
0

What about using eval like here:

import datetime, json
data = {'orgRadar': u'37125110', 'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953), 'clonedRadarsdetailslist': ["{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}", "{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}"]}

data['clonedRadarsdetailslist'] = eval(data['clonedRadarsdetailslist'][0])
print data
  • 3
    eval is probably the easiest approach, but need to trust the data 100% because it could contain malicious executable code, leading to security concerns. – Penguin Brian May 11 '18 at 00:07