Can you help me how I can pass list "some" into string "json_req" using format?
some = ["3874933","38423894"]
json_req='{"marketIds" : {},"priceProjection" : {"priceData" : ["EX_BEST_OFFERS"]}}'.format(some)
The result should be:
'{"marketIds" : ["3874933","38423894"],"priceProjection" : {"priceData" : ["EX_BEST_OFFERS"]}}'
At the moment I receive an error:
KeyError: '"marketIds" '
Thanks
If you have braces in a string you are formatting using format() you need to escape them by using double braces:
json_req='{{"marketIds" : {},"priceProjection" : {{"priceData" : ["EX_BEST_OFFERS"]}}}}'.format(some)
That being said, this whole method of generating the json makes me a little uneasy. It feels like you'd be better off using the json module with a native python object and using dumps at the end.
import json
some = ["3874933","38423894"]
template = {"marketIds" : some, "priceProjection" : {"priceData" : ["EX_BEST_OFFERS"]}}
json_req = json.dumps(template)
That'll lead to more flexible code if you need to change stuff later on.
Related
I have a JSON output like this:
{
":output":{
"response":"{ \"ParentId\" : 125, \"ParentKey\" : { \"key\" : \"9aqews-uwdguwdw8-9uw8\", \"identity\" : \"key_ID=674\" } }"
}
}
I'm trying to fetch the content of key, that is: 9aqews-uwdguwdw8-9uw8
Here are somethings that tried:
------------------------------------------------------
${json_data} Parse Json ${output}
Log ${json_data["output"]["response"]}
Log ${json_data["output"]["response"][0][0:10]}
------------------------------------------------------
${json}= Convert To Dictionary ${values}
${j_keys}= Get Dictionary Keys ${json}
Log ${j_keys}
------------------------------------------------------
${values}= Evaluate json.loads($output) json
Log ${values['output']['response'][1]}
-----------------------------------------------------
${KeySP}= Get Substring ${values} "key" ","
Log ${keySP}
------------------------------------------------------
#${parkeydict}= ${values['output']['response']}
#${keyspacedict}= ${parkeydict['ParentKey']}
#Log ${keyspacedict['key']}
------------------------------------------------------
I have tried several other steps, possibilities and keywords,
The best I could parse is till "Log ${json_data["output"]["response"]}" which returns data from 'response'.
It fails even if I convert to Dict and access the 'key', I think that further data after 'response' is completely stored as values.
Can someone help/guide me on how to capture the data in 'key' variable?
Thanks in Advance!
The first problem is that you're trying to use ['output'], but the key is :output.
The second problem is that the value of the "response" key is not a dictionary, it's a string that looks like a dictionary. You must convert it to a dictionary before you try to pull values from it, assuming it is indeed an well-formed json dictionary and not just a string that might look like a dictionary.
This works for me on the exact data provided in the question:
${values}= Evaluate json.loads($output)
${response}= Evaluate json.loads($values[':output']['response'])
${key}= set variable ${response['ParentKey']['key']}
should be equal ${key} 9aqews-uwdguwdw8-9uw8
Note: if you're using a version of robot that is older than 3.2 you'll need to include json as a final argument for the Evaluate command so that robot knows to import the module. Starting with version 3.2 and onward, modules used in the expression are automatically imported.
I'm using an API that is giving me and output formatted as
['{"quote":{"symbol":"AAPL"', '"companyName":"Apple Inc."', '"primaryExchange":"Nasdaq Global Select"', '"sector":"Technology"', '"calculationPrice":"close"', '"open":367.88', '"openTime":1593696600532', '"close":364.11', '"closeTime":1593720000277', '"high":370.47', '"low":363.64', '"latestPrice":364.11'}]
...(it keeps going like this with many more categories.)
I am attempting to pull out only the latest price. What would be the best way to do that?
This is what I have but I get a bunch of errors.
string = (data.decode("utf-8"))
data_v = string.split(',')
for word in data_v[latestPrice]:
if word == ["latestPrice"]:
print(word)
print(data_v)
Judging by the output this is JSON. To parse this easily use the JSON module (see https://docs.python.org/3/library/json.html ).
If I'm correct you got this output from Yahoo Finance, if this indeed the case don't fetch and parse it manually but use the yfinance module (see https://pypi.org/project/yfinance/ )
You will have to use JSON module to parse this JSON string. You can convert it into dictionary then. I have indented the JSON code for ease of understanding. You can use the following approach,
import json
text_to_parse = """
{"quote":
{
"symbol":"AAPL",
"companyName":"Apple Inc.",
"primaryExchange":"Nasdaq Global Select",
"sector":"Technology",
"calculationPrice":"close",
"open":367.88,
"openTime":1593696600532,
"close":364.11,
"closeTime":1593720000277,
"high":370.47,
"low":363.64,
"latestPrice":364.11
}
}
"""
parsed_dict = json.loads(text_to_parse)
print(parsed_dict["quote"]["latestPrice"])
When the program is run, it outputs 364.11
I am trying to convert an xml file to JSON.
In the python script, I am reading the xml file, store the tags in a dict and then dump into JSON.
The issue is that some of the tag in xml file are optional. As of now i am handling it via IF-conditions. I wanted to check is there is a better way of handling this?
My dict object looks something like this.
In this for example, some entries in the XML may have the tab for Variables, and others may not.
dictData[dictFolder['FOLDER_NAME']][dictJob['JOBNAME']] = {
'Type' : dictJob['JOBTYPE'],
'Command' : dictJob['SCRIPTNAME'],
'Description' : dictJob['DESCRIPTION'],
'When' : {'WeekDays' : dictJob['SCHEDULE'],
'FromTime' : dictJob['FROMTIME']},
'Variables' : [varDict],
'addInCondition' : {'Type': 'WaitForEvents',
'Events' : jobINDict['Events']},
'addOutCondition' : {'Type': 'AddEvents',
'Events' : jobOUTDict['Events']}
}
check out lxml2json (disclosure: I wrote it).
it can convert any xml element into its json equivalent, and gives a measure of control over the structure of the output: including orderedDict, and which elements to render as list.
https://github.com/rparelius/lxml2json
I have a json object saved inside test_data and I need to know if the string inside test_data['sign_in_info']['package_type'] contains the string "vacation_package" in it. I assumed that in could help but I'm not sure how to use it properly or if it´s correct to use it. This is an example of the json object:
"checkout_details": {
"file_name" : "pnc04",
"test_directory" : "test_pnc04_package_today3_signedout_noinsurance_cc",
"scope": "wdw",
"number_of_adults": "2",
"number_of_children": "0",
"sign_in_info": {
"should_login": false,
**"package_type": "vacation_package"**
},
package type has "vacation_package" in it, but it's not always this way.
For now I´m only saving the data this way:
package_type = test_data['sign_in_info']['package_type']
Now, is it ok to do something like:
p= "vacation_package"
if(p in package_type):
....
Or do I have to use 're' to cut the string and find it that way?
You answer depends on what exactly you expect to get from test_data['sign_in_info']['package_type']. Will 'vacation_package' always be by itself? Then in is fine. Could it be part of a larger string? Then you need to use re.search. It might be safer just to use re.search (and a good opportunity to practice regular expressions).
No need to use re, assuming you are using the json package. Yes, it's okay to do that, but are you trying to see if there is a "package type" listed, or if the package type contains vacation_package, possibly among other things? If not, this might be closer to what you want, as it checks for exact matches:
import json
data = json.load(open('file.json'))
if data['sign_in_info'].get('package_type') == "vacation_package":
pass # do something
I have an output :
result = {
"sip_domains":{
"prefix":[{"name":""}],
"domain":[{"name":"k200.com"},{"name":"Zinga.com"},{"name":"rambo.com"}]
},
"sip_security":{"level":2},
"sip_trusted_hosts":{"host":[]},
"sip_proxy_mode":{"handle_requests":1}
}
from this i just wanted the output to print to my screen :
domain : k200.com
domain : Zinga.com
domain : rambo.com
how can i get this output using regular expression
Help needed urgently
If it's the text you need to parse then Use JSON module to parse the JSON payload:
http://docs.python.org/library/json.html?highlight=json#json
Regular expression are not needed with good programming language like Python.
Otherwise if it's Python dictionary then use Python dictionary [] style item access to read data from the dictionary.
If you are getting this data as a string from somewhere you must convert it to a python dictionary object to access it. You should not have to use any regular expressions to get this output.
import json
# get the json str somehow
json_dict = json.loads(json_str)
for domain_dict in json_dict['sip_domains']['domain']:
print 'domain : %s' % (domain_dict['name'])