Parsing a python string into a JSON file
Question:
I need to parse a string from a python function into json.dump() but I can’t find a solution to remove the quotation marks from the string after it’s written into the JSON.
I have this:
"[{'number1':1, 'number2': 2, 'number3': 3, 'word1': 'word'},{'number1':2, 'number2': 2, 'number3': 3, 'word1': 'word'}]"
And I need this:
[{'number1':1, 'number2': 2, 'number3': 3, 'word1': 'word'},{'number1':2, 'number2': 2, 'number3': 3, 'word1': 'word'}]
I tried to strip it using str.strip('""')
but the quotation marks stay and a few different things from other threads similar to my problem but none have worked
Is there a way to accomplish what I am trying to do or is that impossible?
Code:
def batchStr(batchIds, count1, count2 ,status):
seg1 = "{'batchId':"
seg2 = ", 'source1count': "
seg3 = ", 'source2count': "
seg4 = ", 'status':"
seg5 = "},"
str1 = "["
for id in range(len(batchIds)):
str2 = str(seg1)+str(batchIds[id])+str(seg2)+str(count1[id])+str(seg3)+str(count2[id]+str(seg4)+str(status[id])+str(seg5)
str1 = str(str1) + str(str2)
str1 = str1[:-1]+"]"
return str1
I want it to output to the JSON like that:
[{'batchId':1, 'source1count': 100, 'source2count': 100, 'status':success},...]
But it outputs:
"[{'batchId':1, 'source1count': 100, 'source2count': 100, 'status':success},...]"
Answers:
You should convert your list in string format to list first, try using eval(list).
Then, you could do json.dumps(list) and write it into the file.
To use the strip string function you need the backslash escape character for the quotation character.
str = "[{'number1':1, 'number2': 2, 'number3': 3, 'word1': 'word'},{'number1':2, 'number2': 2, 'number3': 3, 'word1': 'word'}]"
solution = str.strip('"')
print(solution)
Or you can use RE regular expression.
It seems the quotemarks you are seeing are just python informing you that the data you are displaying is a string (this will happen if you are using the repl or a jupyter notebook).
Regardless, here is a cleaned up version of your code that outputs what you are looking for
import json
def batchStr(batchIds, count1, count2 ,status):
json_array = [
dict(
batchId=bid,
source1count=c1,
source2count=c2,
status=s
)
for bid, c1, c2, s in
zip(batchIds, count1, count2, status)
]
return json.dumps(json_array)
print(batchStr([1,2,3], [100]*3, [200]*3, ["success"]*3))
Outputs:
[{"batchId": 1, "source1count": 100, "source2count": 200, "status": "success"}, {"batchId": 2, "source1count": 100, "source2count": 200, "status": "success"}, {"batchId": 3, "source1count": 100, "source2count": 200, "status": "success"}]
I need to parse a string from a python function into json.dump() but I can’t find a solution to remove the quotation marks from the string after it’s written into the JSON.
I have this:
"[{'number1':1, 'number2': 2, 'number3': 3, 'word1': 'word'},{'number1':2, 'number2': 2, 'number3': 3, 'word1': 'word'}]"
And I need this:
[{'number1':1, 'number2': 2, 'number3': 3, 'word1': 'word'},{'number1':2, 'number2': 2, 'number3': 3, 'word1': 'word'}]
I tried to strip it using str.strip('""')
but the quotation marks stay and a few different things from other threads similar to my problem but none have worked
Is there a way to accomplish what I am trying to do or is that impossible?
Code:
def batchStr(batchIds, count1, count2 ,status):
seg1 = "{'batchId':"
seg2 = ", 'source1count': "
seg3 = ", 'source2count': "
seg4 = ", 'status':"
seg5 = "},"
str1 = "["
for id in range(len(batchIds)):
str2 = str(seg1)+str(batchIds[id])+str(seg2)+str(count1[id])+str(seg3)+str(count2[id]+str(seg4)+str(status[id])+str(seg5)
str1 = str(str1) + str(str2)
str1 = str1[:-1]+"]"
return str1
I want it to output to the JSON like that:
[{'batchId':1, 'source1count': 100, 'source2count': 100, 'status':success},...]
But it outputs:
"[{'batchId':1, 'source1count': 100, 'source2count': 100, 'status':success},...]"
You should convert your list in string format to list first, try using eval(list).
Then, you could do json.dumps(list) and write it into the file.
To use the strip string function you need the backslash escape character for the quotation character.
str = "[{'number1':1, 'number2': 2, 'number3': 3, 'word1': 'word'},{'number1':2, 'number2': 2, 'number3': 3, 'word1': 'word'}]"
solution = str.strip('"')
print(solution)
Or you can use RE regular expression.
It seems the quotemarks you are seeing are just python informing you that the data you are displaying is a string (this will happen if you are using the repl or a jupyter notebook).
Regardless, here is a cleaned up version of your code that outputs what you are looking for
import json
def batchStr(batchIds, count1, count2 ,status):
json_array = [
dict(
batchId=bid,
source1count=c1,
source2count=c2,
status=s
)
for bid, c1, c2, s in
zip(batchIds, count1, count2, status)
]
return json.dumps(json_array)
print(batchStr([1,2,3], [100]*3, [200]*3, ["success"]*3))
Outputs:
[{"batchId": 1, "source1count": 100, "source2count": 200, "status": "success"}, {"batchId": 2, "source1count": 100, "source2count": 200, "status": "success"}, {"batchId": 3, "source1count": 100, "source2count": 200, "status": "success"}]