WWW::Wunderground::API Can't bless non-reference value - json

I would like to use module WWWW::Wunderground::API to download data with weather using JSON.
Here is my PERL script:
use WWW::Wunderground::API;
my $wun = new WWW::Wunderground::API(location=>'KIAD', api_key=>'my key');
print 'JSON source:'.$wun->json if $wun->api_type eq 'json';
It gives me an error:
Can't bless non-reference value at
/usr/local/share/perl5/Hash/AsObject.pm line 82.
I cannot fix it up. I have been trying to update cpan and other modules but it gives no results.
Could you tell me how can I repair it?
Thank you in advance
with Carp::Always:
Can't bless non-reference value at
/usr/local/share/perl5/Hash/AsObject.pm line 82
Hash::AsObject::AUTOLOAD('Hash::AsObject', undef) called at
/usr/local/share/perl5/WWW/Wunderground/API.pm line 37
WWW::Wunderground::API::update('WWW::Wunderground::API=HASH(0x1e5b178)',
'KIAD') called at /home/xyz/workspace/WeatherTest/scr.pl line 4
eval {...} called at /home/xyz/workspace/WeatherTest/scr.pl line
4

It comes from a lack of error checking. The following unexpectedly returns undef:
JSON::Any->jsonToObj($json)->{current_observation}
You might want to have a look at what's getting fetched by using the following:
use LWP::Simple qw( get );
my $api_key = 'my key';
my $location = 'KIAD';
print get("http://api.wunderground.com/api/$api_key/conditions/q/$location.json");
Perhaps there's an error message in the response you can address. For example, a bad API key would result in the following response:
{
"response": {
"version": "0.1"
,"termsofService": "http://www.wunderground.com/weather/api/d/terms.html"
,"features": {
}
,
"error": {
"type": "keynotfound"
,"description": "this key does not exist"
}
}
}

Related

Parse JSON with missing fields using cjson Lua module in Openresty

I am trying to parse a json payload sent via a POST request to a NGINX/Openresty location. To do so, I combined Openresty's content_by_lua_block with its cjson module like this:
# other locations above
location /test {
content_by_lua_block {
ngx.req.read_body()
local data_string = ngx.req.get_body_data()
local cjson = require "cjson.safe"
local json = cjson.decode(data_string)
local endpoint_name = json['endpoint']['name']
local payload = json['payload']
local source_address = json['source_address']
local submit_date = json['submit_date']
ngx.say('Parsed')
}
}
Parsing sample data containing all required fields works as expected. A correct JSON object could look like this:
{
"payload": "the payload here",
"submit_date": "2018-08-17 16:31:51",
},
"endpoint": {
"name": "name of the endpoint here"
},
"source_address": "source address here",
}
However, a user might POST a differently formatted JSON object to the location. Assume a simple JSON document like
{
"username": "JohnDoe",
"password": "password123"
}
not containing the desired fields/keys.
According to the cjson module docs, using cjson (without its safe mode) will raise an error if invalid data is encountered. To prevent any errors being raised, I decided to use its safe mode by importing cjson.safe. This should return nil for invalid data and provide the error message instead of raising the error:
The cjson module will throw an error during JSON conversion if any invalid data is encountered. [...]
The cjson.safe module behaves identically to the cjson module, except when errors are encountered during JSON conversion. On error, the cjson_safe.encode and cjson_safe.decode functions will return nil followed by the error message.
However, I do not encounter any different error handling behavior in my case and the following traceback is shown in Openresty's error.log file:
2021/04/30 20:33:16 [error] 6176#6176: *176 lua entry thread aborted: runtime error: content_by_lua(samplesite:50):16: attempt to index field 'endpoint' (a nil value)
Which in turn results in an Internal Server Error:
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>openresty</center>
</body>
</html>
I think a workaround might be writing a dedicated function for parsing the JSON data and calling it with pcall() to catch any errors. However, this would make the safe mode kind of useless. What am I missing here?
Your “simple JSON document” is a valid JSON document. The error you are facing is not related to cjson, it's a standard Lua error:
resty -e 'local t = {foo = 1}; print(t["foo"]); print(t["foo"]["bar"])'
1
ERROR: (command line -e):1: attempt to index field 'foo' (a number value)
stack traceback:
...
“Safeness” of cjson.safe is about parsing of malformed documents:
cjson module raises an error:
resty -e 'print(require("cjson").decode("[1, 2, 3"))'
ERROR: (command line -e):1: Expected comma or array end but found T_END at character 9
stack traceback:
...
cjson.safe returns nil and an error message:
resty -e 'print(require("cjson.safe").decode("[1, 2, 3"))'
nilExpected comma or array end but found T_END at character 9

AWS Batch Job container_properties is invalid: Error decoding JSON: invalid character 'v' looking for beginning of value

I'm using terraform to create aws batch job definition:
resource "aws_batch_job_definition" "test" {
name = "jobtest"
type = "container"
container_properties =<<CONTAINER_PROPERTIES
{
"image": var.image,
"memory": 512,
"vcpus": 1,
"jobRoleArn": "${aws_iam_role.job_role.arn}"
}
CONTAINER_PROPERTIES
}
When I run terraform I get this error:
AWS Batch Job container_properties is invalid: Error decoding JSON: invalid character 'v' looking for beginning of value
on admin/prd/batch.tf line 1, in resource "aws_batch_job_definition" "test":
1: resource "aws_batch_job_definition" "test" {
I don't know what's wrong here. I couldn't find any answers in the other StackOverflow questions.

Chef Inspec Test JSON output from an HTTP API

I am trying to create a inspec control where i need to check the status in a json file which gets downloaded when hitting a http url.
When i hit http://localhost:5000/aaa/bbb/ccc/v1/healthCheck url a file gets downloaded(healthCheck.json).
I am trying to execute the below code
control "file_check" do
http_request = http('http://localhost:5000/aaa/bbb/ccc/v1/healthCheck')
describe json(content: http_request.body) do
its('status') { should eq 'success' }
end
end
The error i am getting is
"results": [
{
"status": "failed",
"code_desc": "Control Source Code Error a00972-http/controls/http.rb:5 ",
"run_time": 0.0006573,
"start_time": "2019-06-13T09:56:57+10:00",
"message": "undefined method `body' for nil:NilClass",
"exception": "RuntimeError",
Thanks in advance for the help.
according to the json resource, you are not accessing the json path correctly.
change:
its('status') { should eq 'success' }
to:
its(['results', 0, 'status']) { should eq 'success' }
please note that the value of results[0].success is failed

How to fetch an attribute value from a variable, having the content of a JSON response

I'm using the Robot Framework API automation. Here, storing the JSON response in a variable [POSTResp.content]. I.e., "POSTResp.content" has the whole response, as given below. Please help me to get an attribute's value (for ex, value of referenceId) from the stored content.
Example of JSON response:
{
"serviceResponseHeader": {
"responseContext": {
"responseCode": "MS19",
"responseDescription": "Success",
"serviceResponseTimeInGMT": "18 Sep 2018 16:12:43 GMT"
},
"requesterContext": {
"applicationCode": null,
"applicationSubCode": null,
"countryCode": null,
"requesterReferenceNumber": null,
"requestTimeInGMT": "30 Jun 2015 11:54:49 GMT",
"requesterUserIdentity": "23483",
"requesterGroupIdentity": "1620",
"requesterIpAddress": "",
"sessionIdentity": "2536kjhfdashfkhfsab",
"ssoSessionIdentity": "2536kjhfdashfkhfsab",
"requesterAbbreviatedGroupName": "NEWCOMP"
},
"serviceContext": {
"serviceVersionNumber": "1.0",
"serviceCode": "30"
}
},
"getProxyDetailResponseBody": {
"proxyDetails": {
"proxyType": "",
"proxyValue": "20140005K",
"referenceId": "PR18090000847597",
"transactionId": "18091801657466"
}
}
}
I've tried the below ways,
1) ${json} To JSON ${POSTResp.content} true
log to console \n the Proxy ID is ${json["proxyValue"]}
Result: Resolving variable '${json["proxyValue"]}' failed: TypeError: string indices must be integers, not str
2) ${json} Evaluate json.loads(${POSTResp.content}} json
log to console \n the Proxy ID is ${json["proxyValue"]}
Result: failed: SyntaxError: unexpected EOF while parsing (, line 1)
Issues with your two approaches:
1) the library keyword call passes a true argument (well, truth-like) to the pretty_print parameter:
${json} To JSON ${POSTResp.content} true
Looking at the library's source, in that case the keyword does not return a dict object - but a string, a beatified version of the source json. That coincides with the error your received.
Remove the "true" argument and it must return a dict.
2) In the Evaluate surround the variable with triple quotes (python's literal string):
${json} Evaluate json.loads('''${POSTResp.content}'''}
json
Without it, the framework just dumped the variable's value, which raised a python syntax error.
By the way, try not to make your variables with language keywords/library names - like ${json} up there.

Ruby: Handling different JSON response that is not what is expected

Searched online and read through the documents, but have not been able to find an answer. I am fairly new and part of learning Ruby I wanted to make the script below.
The Script essentially does a Carrier Lookup on a list of numbers that are provided through a CSV file. The CSV file has just one row with the column header "number".
Everything runs fine UNTIL the API gives me an output that is different from the others. In this example, it tells me that one of the numbers in my file is not a valid US number. This then causes my script to stop running.
I am looking to see if there is a way to either ignore it (I read about Begin and End, but was not able to get it to work) or ideally either create a separate file with those errors or just put the data into the main file.
Any help would be much appreciated. Thank you.
Ruby Code:
require 'csv'
require 'uri'
require 'net/http'
require 'json'
number = 0
CSV.foreach('data1.csv',headers: true) do |row|
number = row['number'].to_i
uri = URI("https://api.message360.com/api/v3/carrier/lookup.json?PhoneNumber=#{number}")
req = Net::HTTP::Post.new(uri)
req.basic_auth 'XXX' , 'XXX'
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
http.request(req)
}
json = JSON.parse(res.body)
new = json["Message360"]["Carrier"].values
CSV.open("new.csv", "ab") do |csv|
csv << new
end
end
File Data:
number
5556667777
9998887777
Good Response example in JSON:
{"Message360"=>{"ResponseStatus"=>1, "Carrier"=>{"ApiVersion"=>"3", "CarrierSid"=>"XXX", "AccountSid"=>"XXX", "PhoneNumber"=>"+19495554444", "Network"=>"Cellco Partnership dba Verizon Wireless - CA", "Wireless"=>"true", "ZipCode"=>"92604", "City"=>"Irvine", "Price"=>0.0003, "Status"=>"success", "DateCreated"=>"2018-05-15 23:05:15"}}}
The response that causes Script to stop:
{
"Message360": {
"ResponseStatus": 0,
"Errors": {
"Error": [
{
"Code": "ER-M360-CAR-111",
"Message": "Allowed Only Valid E164 North American Numbers.",
"MoreInfo": []
}
]
}
}
}
It would appear you can just check json["Message360"]["ResponseStatus"] first for a 0 or 1 to indicate failure or success.
I'd probably add a rescue to help catch any other errors (malformed JSON, network issue, etc.)
CSV.foreach('data1.csv',headers: true) do |row|
number = row['number'].to_i
...
json = JSON.parse(res.body)
if json["Message360"]["ResponseStatus"] == 1
new = json["Message360"]["Carrier"].values
CSV.open("new.csv", "ab") do |csv|
csv << new
end
else
# handle bad response
end
rescue StandardError => e
# request failed for some reason, log e and the number?
end