Python output a json style string - python

here defining a variable:
sms_param = '{\"website\":\"hello\"}'
and it print out ok like this : {"website":"hello"}, but i want to pass a dynamic value to its value, so its format should like this: {\"website\":\"{0}\"}.format(msg), but it output a KeyError, I have no idea of this Error, and change all kinds of string format such as triple quotation and change {0} with %s, but all seems useless. how can i solve it.

My suggestion is using json.loads()
>>> sms_param = '{\"website\":\"hello\"}'
>>> import json
>>> json.loads(sms_param)
{'website': 'hello'}
What you can do is using json.loads() convert the json string to dictionary and then change the value, finally convert it back to string

Related

Converting dictionary string to dictionary failed

I tried to convert below string to dictionary by json.loads() and ast.literal_eval method but both failed, please kindly help:
text = '{"data":{"FormId":"BD_Rate","TopRowCount":0,"Limit":0,"StartRow":0,"FilterString":'',"OrderString":'',"FieldKeys":"FRateID,FDocumentStatus,FForbidStatus,FName,FDescription,FCreateOrgId,FUseOrgId,FCreatorId,FModifierId,FCreateDate,FModifyDate,FBegDate,FCyToID,FEndDate,FRATETYPEID,FCyForID,FAuditDate,FAuditorID,FForbidderID,FForbidDate,FIsSysPreset,FExchangeRate,FReverseExRate"}}'
Your string is quoted using single quotes (') and you also have "OrderString":'' as part of your string. Python will remove those '' because it will assume string concatenation. For example:
s = 'foo''bar'
is equivalent to
s = 'foo' + 'bar'
hence the same thing as
s = 'foobar'
Change your first line to
text = '{"data":{"FormId":"BD_Rate","TopRowCount":0,"Limit":0,"StartRow":0,"FilterString":"","OrderString":"","FieldKeys":"FRateID,FDocumentStatus,FForbidStatus,FName,FDescription,FCreateOrgId,FUseOrgId,FCreatorId,FModifierId,FCreateDate,FModifyDate,FBegDate,FCyToID,FEndDate,FRATETYPEID,FCyForID,FAuditDate,FAuditorID,FForbidderID,FForbidDate,FIsSysPreset,FExchangeRate,FReverseExRate"}}'
and both json and ast.literal_eval will work.

json.loads change " " to single quote

I am writing a program to call an API. I am trying to convert my data payload into json. Thus, I am using json.loads() to achieve this.
However, I have encountered the following problem.
I set my variable as following:
apiVar = [
"https://some.url.net/api/call", #url
'{"payload1":"email#user.net", "payload2":"stringPayload"}',#payload
{"Content-type": "application/json", "Accept": "text/plain"}#headers
]
Then I tried to convert apiVar[1] value into json object.
jsonObj = json.loads(apiVar[1])
However, instead of giving me output like the following:
{"payload1":"email#user.net", "payload2":"stringPayload"}
It gives me this instead:
{'payload1':'email#user.net', 'payload2':'stringPayload'}
I know for sure that this is not a valid json format. What I would like to know is, why does this happen? I try searching a solution for it but am not able to find anything on it. All code examples suggest it should have given me the double quote instead.
How should I fix it so that it will give the double quote output?
json.loads() takes a JSON string and converts it into the equivalent Python datastructure, which in this case is a dict containing strings. And Python strings display in single quotes by default.
If you want to convert a Python datastructure to JSON, use json.dumps(), which will return a string. Or if you're outputting straight to a file, use json.dump().
In any case, your payload is already valid JSON, so the only reason to load it is if you want to make changes to it before calling the API.
You need to use the json.dumps to convert the object back into json format.
The string with single quotes that you are reverencing is probably a str() or repr() method that is simply used to visualize the data as a python object (dictionary) not a json object. try taking a look at this:
print(type(jsonObj))
print(str(jsonObj))
print(json.dumps(jsonObj))

Escape double quotes when converting a dict to json in Python

I need to escape double quotes when converting a dict to json in Python, but I'm struggling to figure out how.
So if I have a dict like {'foo': 'bar'}, I'd like to convert it to json and escape the double quotes - so it looks something like:
'{\"foo\":\"bar\"}'
json.dumps doesn't do this, and I have tried something like:
json.dumps({'foo': 'bar'}).replace('"', '\\"') which ends up formatting like so:
'{\\"foo\\": \\"bar\\"}'
This seems like such a simple problem to solve but I'm really struggling with it.
Your last attempt json.dumps({'foo': 'bar'}).replace('"', '\\"') is actually correct for what you think you want.
The reason you see this:
'{\\"foo\\": \\"bar\\"}'
Is because you're printing the representation of the string. The string itself will have only a single backslash for each quote. If you use print() on that result, you will see a single backslash
What you have does work. Python is showing you the literal representation of it. If you save it to a variable and print it shows you what you're looking for.
>>> a = json.dumps({'foo': 'bar'}).replace('"', '\\"')
>>> print a
{\"foo\": \"bar\"}
>>> a
'{\\"foo\\": \\"bar\\"}'

Json object is printed in strange format in Python

so i do this get request to a Steam page where it responds this JSON:
{"success":true,"lowest_price":"$2.23","volume":"2,842","median_price":"$2.24"}
My objective is to transform it into a dictionary in Python, but what i get when i return the JSON object in my function is this:
{u'volume': u'2,842', u'median_price': u'2,02€ ',
u'lowest_price': u'1,99€ ', u'success': True} (notice the u').
What can i do to eliminate the u's?
You're seeing Python letting you know that the strings you're printing are unicode strings. Unless the output you're seeing really matters (e.g., it's input for something else), you can generally disregard the leading 'u' character until you run into issues with unicode output.
There are a litany of stack overflow questions which address this.
Python string prints as [u'String']
What's the u prefix in a python string
Printing a string prints 'u' before the string in Python?
And a lot more....
You could import json module and use json.dumps to prettify your output.
import json
response = {"success":True,"lowest_price":"$2.23","volume":"2,842","median_price":"$2.24"}
print json.dumps(response, indent=2)

Convert unicode string dictionary into dictionary in python

I have unicode u"{'code1':1,'code2':1}" and I want it in dictionary format.
I want it in {'code1':1,'code2':1} format.
I tried unicodedata.normalize('NFKD', my_data).encode('ascii','ignore') but it returns string not dictionary.
Can anyone help me?
You can use built-in ast package:
import ast
d = ast.literal_eval("{'code1':1,'code2':1}")
Help on function literal_eval in module ast:
literal_eval(node_or_string)
Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.
You can use literal_eval. You may also want to be sure you are creating a dict and not something else. Instead of assert, use your own error handling.
from ast import literal_eval
from collections import MutableMapping
my_dict = literal_eval(my_str_dict)
assert isinstance(my_dict, MutableMapping)
EDIT: Turns out my assumption was incorrect; because the keys are not wrapped in double-quote marks ("), the string isn't JSON. See here for some ways around this.
I'm guessing that what you have might be JSON, a.k.a. JavaScript Object Notation.
You can use Python's built-in json module to do this:
import json
result = json.loads(u"{'code1':1,'code2':1}") # will NOT work; see above
I was getting unicode error when I was reading a json from a file. So this one worked for me.
import ast
job1 = {}
with open('hostdata2.json') as f:
job1= json.loads(f.read())
f.close()
#print type before converting this from unicode to dic would be <type 'unicode'>
print type(job1)
job1 = ast.literal_eval(job1)
print "printing type after ast"
print type(job1)
# this should result <type 'dict'>
for each in job1:
print each
print "printing keys"
print job1.keys()
print "printing values"
print job1.values()
You can use the builtin eval function to convert the string to a python object
>>> string_dict = u"{'code1':1, 'code2':1}"
>>> eval(string_dict)
{'code1': 1, 'code2': 1}

Categories

Resources