I'm trying to parse the test_string to JSON but failing, just continues as string after json.dumps, see result below. How can I get the expected output?
The test_string is pulled from a text object and that's how it's quoted.
test_string = "{'fruit': ['Yes'], 'vegetables': ['carrot']}"
# output
json.dumps(test)
'"{\'fruit\': [\'Yes\'], \'vegetables\': [\'carrot\']}"'
# expected output
{'fruit': ['Yes'], 'vegetables': ['carrot']}
use ast and json
import ast
import json
data = ast.literal_eval("{'fruit': ['Yes'], 'vegetables': ['carrot']}")
print(json.dumps(data))
output
{"fruit": ["Yes"], "vegetables": ["carrot"]}
json.loads() should do the trick
test_string = "{'fruit': ['Yes'], 'vegetables': ['carrot']}"
new_test_string = json.loads(test_string)
Related
I need a little help processing a String to a Dict, considering that the String is not in a common format, but an output from a UDF function
The return from the PySpark UDF looks like the string below:
"{list=[{a=1}, {a=2}, {a=3}]}"
And I need to convert it to a python dictionary with the structure below:
{
"list": [
{"a": 1}
{"a": 2}
{"a": 3}
]
}
So I can access it's values, like
dict["list"][1]["a"]
I already tried using:
JSON.loads
ast_eval()
Could someone please help me?
As an example of how this unparsed string is generated:
#udf()
def execute_method():
return {"list": [{"a":1},{"b":1}{"c":1}]}
df_result = df_source.withColumn("result", execute_method())
By the very least you will need to replace = with : and surround keys with double quotes:
import json
import re
string = "{list=[{a=1}, {a=2}, {a=3}]}"
fixed_string = re.sub(r'(\w+)=', r'"\1":', string)
print(type(fixed_string), fixed_string)
parsed = json.loads(fixed_string)
print(type(parsed), parsed)
outputs
<class 'str'> {"list":[{"a":1}, {"a":2}, {"a":3}]}
<class 'dict'> {'list': [{'a': 1}, {'a': 2}, {'a': 3}]}
try this :
import re
import json
data="{list=[{a=1}, {a=2}, {a=3}]}"
data=data.replace('=',':')
pattern=[e.group() for e in re.finditer('[a-z]+', data, flags=re.IGNORECASE)]
for e in set(pattern):
data=data.replace(e,"\""+e+"\"")
print(json.loads(data))
I have a list of this order
text = ['code1','matrix result from one','code2','matrix result from two']
I want output in json as :
text = {"code1" : "matrix result from one", "code2" : "matrix result from two"}
How can i do this? Thanks in advance
Use json.dumps() to convert data to a string of a JSON object and json.loads() to create a JSON object.
import json
text = ['code1','matrix result from one','code2','matrix result from two']
json_dump = json.dumps(text)
print(json_dump)
I am trying to combine two json files but end with some weird "\" in my output.
import json
data1 = {'apple': 'good',"mango": "excellent"}
json_data1 = json.dumps(data1)
data2 = {'mustang': 'good',"camaro": "excellent"}
json_data2 = json.dumps(data2)
final_data = { 'fruit' : str(json_data1), 'car' : str(json_data2) }
json_final = json.dumps(final_data)
print(json_final)
Expectation:
{"fruit": "{"apple": "good", "mango": "excellent"}", "car": "{"camaro": "excellent", "mustang": "good"}"}
What I got:
{"fruit": "{\"apple\": \"good\", \"mango\": \"excellent\"}", "car": "{\"camaro\": \"excellent\", \"mustang\": \"good\"}"}
How do I overcome this issue?
Also, in my actual problem, I just get two JSON objects and I have no control over anything else.
If Your Data Is Native Python Structures
Don't stringify or JSON-encode the included content separately at all. Keep your content as purely native data structures and only encode as JSON once.
Otherwise, when you run the first json.dumps() pass you're generating a string -- and when you call json.dumps() in a data structure that includes that string, you're generating a JSON sequence that encodes the string, not a JSON data structure that encodes the original dictionary that string was created to represent.
import json
data1 = {'apple': 'good',"mango": "excellent"}
data2 = {'mustang': 'good', "camaro": "excellent"}
final_data = { 'fruit' : data1, 'car' : data2 }
json_final = json.dumps(final_data)
If Your Input Is Already JSON-Encoded...
The safe approach is to decode into native structures before reencoding. That is:
json_data1 = '{"mango": "excellent", "apple": "good"}'
json_data2 = '{"camaro": "excellent", "mustang": "good"}'
final_data = { 'fruit': json.loads(json_data1), 'car': json.loads(json_data2) }
json_final = json.dumps(final_data)
The unsafe approach is to use string concatenation:
# DANGER: Will produce badly-formed output instead of throwing an exception if input is bad
json_data1 = '{"mango": "excellent", "apple": "good"}'
json_data2 = '{"camaro": "excellent", "mustang": "good"}'
json_final = '{ "fruit": %s, "car": %s }' % (json_data1, json_data2)
I have a dictionary:
data = {"data": "\u512b"}
while I dump that to json:
import json
print json.dumps(data)
I got:{"a":"\\u512b"}
What should I do to get exactly {"a":"\u512b"}?
NOTE: I try to add u before the string so it becomes u'\u512b' and the extra \ won't show up again, please also tell me why
You can do some hacking.
import json
data = {"data": "\u512b"}
s = json.dumps(data)
print(s.replace(r'\u', 'u'))
print(type(s.replace(r'\u', 'u')))
Output:
{"data": "\u512b"}
<type 'str'>
My guess is that you are just confused by the output of the Python interpreter, displaying you the json.dumps generated string with its own \ escape character prepended to the \ character in the string. The JSON string as a value contains exactly one \, as you want (IIUC):
>>> data = {"data": "\u512b"}
>>> data
{'data': '\u512b'}
>>> import json
>>> json.dumps(data)
'{"data": "\\u512b"}'
>>> print(json.dumps(data))
{"data": "\u512b"}
>>> json.dump(data, open('data.json', 'w'))
>>> ^Z
C:\opt\Console2>type data.json
{"data": "\u512b"}
This is entirely independent of JSON in fact, as the following example shows:
>>> s = "s\\u"
>>> s
's\\u'
>>> print(s, len(s)) # length of s is 3, not 4
s\u 3
HTH!
I'm trying to convert an string into json output from local Data or Those datas from BeautifulSoup output as Json.for example:
#! /usr/bin/python
data = ('Hello')
print data
and i need to convert this Hello as json output.
How can do that?
is this possible?
Check out the json module in Python https://docs.python.org/2/library/json.html
import json
json.dumps({"hello": 0}, sort_keys=True)
You can use the json module to encode Python objects as JSON, e.g.
>>> import json
>>> data = ('Hello')
>>> json.dumps(data)
'"Hello"'
>>> data = ('Hello', 'There')
>>> json.dumps(data)
'["Hello", "There"]'
>>> data = {'message': 'Hello'}
>>> json.dumps(data)
'{"message": "Hello"}'