malformed JSON from Google directions API when calling from R - json

This might be a duplicate- but I couldn't find something to solve this problem.
I'm using httpGET() to call the google directions API.
Packages:
require(RCurl)
require(rjson)
require(gooJSON)
the code is:
url = "http://maps.googleapis.com/maps/api/directions/json?origin=12.9673293,77.7173975&destination=12.9373613,77.700985&waypoints=optimize:true|12.9723379,77.7117611|12.9922162,77.715895|12.9629354,77.7122996&sensor=false"
routeJSON = httpGET(url= url)
routeList = fromJSON(routeJSON)
I get:
Error in fromJSON(routeJSON) :
unexpected escaped character '\]' at pos 18
I wrote the JSON to a file and copied it to jsoneditoronline.com. I got:
Error: Parse error on line 51:
... "points" : "qscnA_djyMj#kAT[\]\
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
validated by jsonlint
But it works when I put the URL into the browser and copy the output to jsoneditoronline.
Any idea why it is happening and/or how to circumvent it?
EDIT: I tried gooJSON, but it seems it does not support the maps API V3.
> goomap(url)
$Status
$Status$code
[1] 610
$Status$request
[1] "geocode"
$Status$error_message
[1] "The Geocoding API v2 has been turned down on September 9th, 2013. The Geocoding API v3 should be used now. Learn more at https://developers.google.com/maps/documentation/geocoding/"

The following works fine for me:
require(rjson)
url = "http://maps.googleapis.com/maps/api/directions/json?origin=12.9673293,77.7173975&destination=12.9373613,77.700985&waypoints=optimize:true|12.9723379,77.7117611|12.9922162,77.715895|12.9629354,77.7122996&sensor=false"
fromJSON(file=url)
However, if there is invalid json data, one can call
fromJSON(url, unexpected.escape="keep")
to to treat the escaped character as normal character (e.g. \] becomes ])

Related

Encoding works for 1 and not for other list in Twitter using python

I am trying to start cheating data from twitter using twitter module and python. Here's is my code
import twitter
import win_unicode_console
win_unicode_console.enable()
CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxx'
OAUTH_TOKEN = 'xxxxxxxxxxxxxxxxx'
OAUTH_TOKEN_SECRET = 'xxxxxxxxxxxx'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
print(twitter_api)
WORLD_WOE_ID = 1
US_WOE_ID = 23424977
world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)
print(us_trends)
print(world_trends)
I was getting encoding error . so i used
print((us_trends).encode('utf-8'))
which resulted in
AttributeError: 'TwitterListResponse' object has no attribute 'encode'
so i decided to use win_unicode_console module
But whats confusing is that us_trends is returning value.
[{'trends': [{'name': 'El Chapo', 'url': 'http://twitter.com/search?q=%22El+Chapo%22', 'promoted_content': None, 'query': '%22El+Chapo%22', 'tweet_volume': 103536}, {'name': 'Antonio Brown', 'url': 'http://twitter.com/search?q=%22Antonio+Brown%22', 'promoted_
but the statement
print(world_trends)
gives below error
File "C:\Users\nawendu\Desktop\TWIT.PY", line 25, in <module>
print(world_trends)
File
line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 24-
29: character maps to <undefined>
How can the encoding work in us trends and not in world trends??
encode is a method of a string.
You have a json object, it doesn't have this method.
When you print an object it needs to convert the object to a string representation for your output encoding (probably windows encoding here). If there are characters in there (e.g. Emoji) that are not in the output encoding then you get an error.
Encodings are a difficult topic (and a pain point in Python), but you'll need to learn about them if you want to print output.

Invalid JSON - Cannot see why

The following JSON string comes back as invalid in several formatter tests. I cannot figure out for the life of me what is wrong!
{
"draw": 122,
"recordsTotal": 1496,
"recordsFiltered": 1496,
"data": [["11315","1403","John Doe","Parking Pass","-","Hyundai Sonata - Grey ABC 123 ","09-01-2016 2:00 AM","09-01-2016 7:00 AM","-"]]
}
https://jsonformatter.org/ is saying:
Parse error on line 5: ...,"Parking Pass","-","Hyundai Sonata - Gr
-----------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
https://jsonformatter.curiousconcept.com/ says:
Error:Invalid characters found.[Code 18, Structure 28]
and highlights the line "Hyundai Sonata - Grey ABC 123 ",
What the heck am I missing? I dont see any invalid characters anywhere. If I paste it into Notepad++ its not showing anything but "CRLF" on the line breaks.
After checking, it turns out that the value, which was being returned from a database, had a tab in it. I created a function to strip out any non ASCII chars and its working fine now

Escape a hypen in JSON response from Mule

I am using Anypoint Studio 6.1 and Mule 3.8.1 and have the following JSON response from a Mule workflow:
{
"Description": "Top 10 games:
- Mario Kart
- Legend of Zelda"
}
This fails validation with the following error:
Error: Parse error on line 2:
{ "Description": "Top 10 games: - M
-----------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
I have tried escaping the special characters put it still does not work. How can I change the response to pass JSON v4 validation. The JSON validator I have been using is http://jsonlint.com/
Thanks
The root cause of the issue is carriage return in the JSON response. The valid JSON should be as below
{
"Description": "Top 10 games:\r\n- Mario Kart\r\n- Legend of Zelda"
}
Please provide more details on where you are receiving this description. is it from XML or any other source. Also it will be helpful if you post your DWL code as well.

httr GET operation unable to access JSON response

I am trying to access the JSON response from an API call in my R script. The API call is succesful, and I can view the JSON response in the console. However, I am unable to access any data from it.
A sample code segment is:
require(httr)
target <- '#trump'
sentence<- 'Donald trump has a wonderful toupe, it really is quite stunning that a man can be so refined and elegant'
query <- url_encode(sentence)
target <- gsub('#', '', target)
endpoint <- "https://alchemy.p.mashape.com/text/TextGetTargetedSentiment?outputMode=json&target="
apiCall <- paste(endpoint, target, '&text=', query, sep = '')
resp <-GET(apiCall, add_headers("X-Mashape-Key" = sentimentKey, "Accept" = "application/json"))
stop_for_status(resp)
headers(resp)
str(content(resp))
content(resp, "text")
I followed examples in the httr quickstart guide from CRAN (here) as well as this stack.
Unfortunately, I keep getting either "unused parameters 'text' in content()" or "no definition exists for content() accepting a class of 'response.' Does anyone have any advice? PS the headers will print, and resp$content will print the raw bitstream
Expanding on the comment, you need to set the content type explicitly in the call to content(...). Since your code is not reproducible, here is an example using the Census Bureau's geocoder (which returns a json response).
library(httr)
url <- "http://geocoding.geo.census.gov/geocoder/locations/onelineaddress"
resp <-GET(url, query=list(address="1600 Pennsylvania Avenue, Washington DC",
benchmark=9,
format="json"))
json <- content(resp, type="application/json")
json$result$addressMatches[[1]]$coordinates
# $x
# [1] -77.038025
#
# $y
# [1] 38.898735
Assuming your are actually getting a json response, and that it is well-formed, simply using content(resp, type="application/json") should work.

What is the limit exceeded error code or ErrorMessage retrieved as a JSONObject in yahoo api?

I found this error message for yahoo api when the limit is exceeded can anyone tell me what it would return as a Json object and not in xml format?
From the geocoder gem
##
# Yahoo returns errors as XML even when JSON format is specified.
# Handle that here, without parsing the XML
# (which would add unnecessary complexity).
# Yahoo auth errors can also be cryptic, so add raw error desc
# to warning message.
#
def parse_raw_data(raw_data)
if raw_data.match(/^<\?xml/)
if raw_data.include?("Rate Limit Exceeded")
raise_error(Geocoder::OverQueryLimitError) || warn("Over API query limit.")
elsif raw_data =~ /<yahoo:description>(Please provide valid credentials.*)<\/yahoo:description>/i
raise_error(Geocoder::InvalidApiKey) || warn("Invalid API key. Error response: #{$1}")
end
else
super(raw_data)
end
end
So it seems that you question cannot be answered because it will not return a jSON response from Yahoo API. It will always return XML format