Python - Parse requests response - javascript

I've an endpoint where I post the data.
r = requests.post(url, data=data, headers=headers)
I get the following response in Javascript:-
throw 'allowScriptTagRemoting is false.';
(function() {
var r = window.dwr._[0];
//#DWR-INSERT
//#DWR-REPLY
r.handleCallback("1", "0", {
msg: "",
output: {
msg: "Showing from city center",
resultcode: 1,
scrpresent: true,
srclatitude: "28.63244546123956",
srclongitude: "77.21981048583984",
},
result: "success"
});
})();
How do I parse the response? I basically want the output json. HOw can I get the same?

The problem is - output is not a valid JSON string and cannot be loaded via json.loads().
I would use regular expressions to locate the output object and then use findall() to locate key-value pairs. Sample working code:
import re
data = """
throw 'allowScriptTagRemoting is false.';
(function() {
var r = window.dwr._[0];
//#DWR-INSERT
//#DWR-REPLY
r.handleCallback("1", "0", {
msg: "",
output: {
msg: "Showing from city center",
resultcode: 1,
scrpresent: true,
srclatitude: "28.63244546123956",
srclongitude: "77.21981048583984",
},
result: "success"
});
})();
"""
output_str = re.search(r"output: (\{.*?\}),\n", data, re.DOTALL | re.MULTILINE).group(1)
d = dict(re.findall(r'(\w+): "?(.*?)"?,', output_str))
print(d)
Prints:
{'msg': 'Showing from city center', 'resultcode': '1', 'srclongitude': '77.21981048583984', 'srclatitude': '28.63244546123956', 'scrpresent': 'true'}

Related

FaunaDB FQL - No form/function found, or invalid argument keys: { params }

I'm learning FQL and trying to do a mass update, but I can't figure out what I'm doing wrong, nor can I really figure out what the error is really pointing to.
Here is my code:
const updateResult = await serverClient.query(
q.Map(
guests,
q.Lambda(
"guest",
q.Update(q.Var("guest").id, {
data: {
emailSent: q.Var("guest").emailSent,
emailStatus: q.Var("guest").emailStatus,
emailRejectReason: q.Var("guest").emailRejectReason,
},
})
)
)
);
Here is the what the guests object is via console.log:
[ { email: 'myemail+bart72320#gmail.com',
emailStatus: 'sent',
emailRejectReason: null,
emailSent: true,
id: Ref(Collection("Guests"), "271884343706649107") } ]
Here is that same object with JSON.stringify:
[
{
"email": "myemail+bart72320#gmail.com",
"emailStatus": "sent",
"emailRejectReason": null,
"emailSent": true,
"id": {
"#ref": {
"id": "271884343706649107",
"collection": {
"#ref": {
"id": "Guests",
"collection": {
"#ref": {
"id": "collections"
}
}
}
}
}
}
}
]
Here is part of the error that is returned:
{ [BadRequest: invalid expression]
name: 'BadRequest',
message: 'invalid expression',
description:
'No form/function found, or invalid argument keys: { params }.',
requestResult:
RequestResult {
method: 'POST',
path: '',
query: null,
requestRaw:
'{"map":{"lambda":"guest","expr":{"params":{"object":{"data":{"object":{}}}}}},"collection":[{"object":{"email":"myemail+bart72320#gmail.com","emailStatus":"sent","emailRejectReason":null,"emailSent":true,"id":{"#ref":{"id":"271884343706649107","collection":{"#ref":{"id":"Guests","collection":{"#ref":{"id":"collections"}}}}}}}}]}',
requestContent: Expr { raw: [Object] },
responseRaw:
'{"errors":[{"position":["map","expr"],"code":"invalid expression","description":"No form/function found, or invalid argument keys: { params }."}]}',
I've gotten updates to work and lambdas to work, but not this one and maybe I'm just bad at reading very nested, functional looking error messages. What is a form and is it the same as a function, or am I missing keys: params? I don't know what to make of this.
Please help me understand what I'm doing wrong and if this error message is actually helpful and how to interpret it or if it's just a confusing catchall?
Thanks!
You are mixing Javascript syntax with FQL expressions.
Var("guest")
is a FQL expression, but
Var("guest").id
is a Javascript syntax. The equivalent of dot operator in FQL is
Select("id", Var("guest"))
Remember that FQL is not executed on Javascript, but is executed on server side.

How to filter Twit (API) JSON response in node.js?

I'm currently writing a small Twitter app using the Twit API. To do what I need to do, I'd like the data to be able to be filtered by user id, and not get all the other garbage JSON spits out. Here's what the response looks like:
{ created_at: 'Sat Jun 23 03:45:13 +0000 2018',
id: 1010368149466697700,
id_str: '1010368149466697728',
text:
'RT #ClassicIsComing: "Let\'s Talk ETC!" Podcast Series by #chris_seberino of #InputOutputHK \nA deep series of powerful intervie
ws with influ…',
truncated: false,
entities:
{ hashtags: [],
symbols: [],
user_mentions: [ [Object], [Object], [Object] ],
urls: [] },
source:
'TweetDeck',
in_reply_to_status_id: null,
in_reply_to_status_id_str: null,
in_reply_to_user_id: null,
in_reply_to_user_id_str: null,
in_reply_to_screen_name: null,
user:
{ id: 759252279862104000,
id_str: '759252279862104064',
name: 'Ethereum Classic',
screen_name: 'eth_classic',
location: 'Blockchain',
description:
'Latest News and Information from Ethereum Classic (ETC). A crypto-currency with smart contracts which respects immutability a
nd neutrality.',
url: ,
entities: { url: [Object], description: [Object] },
protected: false,
followers_count: 216255,
friends_count: 538,
listed_count: 2147,
etc. The code i'm using to get this is:
T.get('statuses/home_timeline', {count: 1, exclude_replies: true},
function(err, data, response){
if (err){
console.log('Uh oh, we got a problem');
}
else{
console.log('We GUUCie bruh');
}
var tweets = data;
/* for (var i = 0; i < tweets.length; i++) {
console.log(tweets[i]);
} */
console.log(data);
});
the last block of code is commented out because I've attempted to define "tweets" as data.id, data.statuses.id, etc, but everything seems to spit out "undefined." I'm a complete noob to javascript and JSON as I'm only currently learning C++ # school, so any help would be appreciated!
edit
I thought I'd add in the error message to show you what happens when I try to treat the data as an object.
If I try to use JSON.parse(data) as the value for my tweet variable:
T.get('statuses/home_timeline', {count: 1, exclude_replies: true}, callBackFunction)
function callBackFunction(err, data, response){
if (err){
console.log('Uh oh, we got a problem');
}
else{
console.log('We GUUCie bruh');
}
var tweets = JSON.parse(data);
//for (var i = 0; i < tweets.length; i++) {
// console.log(tweets[i].id_str);
// }
console.log(tweets.id_str);
}
I get:
$ node crypt.js
the bot is starting
We GUUCie bruh
undefined:1
[object Object]
^
SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
If I try to treat it as an object right away, with:
function callBackFunction(err, data, response){
if (err){
console.log('Uh oh, we got a problem');
}
else{
console.log('We GUUCie bruh');
}
var tweets = data.id_str;
//for (var i = 0; i < tweets.length; i++) {
// console.log(tweets[i].id_str);
// }
console.log(tweets);
}
I get:
$ node crypt.js
the bot is starting
We GUUCie bruh
undefined
Have you tried JSON.parse?
So your line "var tweets = data;" would be "var tweets = JSON.parse(data);"
From there you should be able to interact with the data as if it were an object and grab specifically the id or whatever you're looking for.
I'm also a noob, so I don't have an in depth explanation as to why this works, but it helped fix an issue I had when pulling data from API.

Object to JSON angularjs

I have this method
function submit() {
var JSONObject = {
"name":$rootScope.name,
"surname":$rootScope.surname,
"email":$rootScope.email,
"review":$rootScope.review
};
debugger;
var Results = UniversalService.PostReview(JSONObject);
}
which I want to post to database later and I get error: SyntaxError: Unexpected token o in JSON at position 0
As I understood I get incorrect JSON format as my console.log input shows :
Object {name: "hh", surname: "hh", email: "kal#gmail.com", review: "fre"}
Instead it should show : "name": "hh" info like this right? How do I change that?

Replying to a tweet using twit and Node.js

I am using the Twit Node.js API.
What I am trying to do is reply to a tweet that matches a certain keyword. I want my reply tweet to show up within the other tweet underneath. Like when you reply via app or website.
My code to do that is here:
var reply = function(tweet) {
var res = {
status: 'This is a tweet',
in_reply_to_status_id: tweet.id_str
};
twitter.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}
The status is written correctly to the reply JSON but in_reply_to_status_id remains null. The tweet is posted to the bots account but not in reply to the tweet is is supposed to be replying to.
Why dos it not work?
And yes I have tried to write to in_reply_to_status_id_str and I have tryed to make the tweet.id_str a string.
I there anyone who knows what I am missing?
Thank you!
My response JSON is here:
{ created_at: 'Wed Jun 21 07:44:22 +0000 2017',
id: 877431986071142400,
id_str: '877431986071142400',
text: 'This is a tweet',
truncated: false,
entities: { hashtags: [], symbols: [], user_mentions: [], urls: [] },
source: 'Spatinator',
in_reply_to_status_id: null,
in_reply_to_status_id_str: null,
in_reply_to_user_id: null,
in_reply_to_user_id_str: null,
in_reply_to_screen_name: null,
If you need more of the respone JSON let me know.
The solution is to include a mention to tweet.user.screen_name into the status of the response json.
Like this it works:
var reply = function(tweet) {
var res = {
status: 'This is a tweet #' + tweet.user.screen_name,
in_reply_to_status_id: '' + tweet.id_str
};
twitter.post('statuses/update', res,
function(err, data, response) {
console.log(data);
}
);
}

ExtJS - SyntaxError: missing ) in parenthetical

I am writing some code to educate myself in the ways of ExtJS. I am also new to JSON so hopefully this question will be easy for you to answer. I am trying to retrieve some data from a basic web service that I have written which should be returning its results as JSON (seeing as I am new to JSON - it could be that that is broken).
The error I am getting is
SyntaxError: missing ) in
parenthetical
The JSON that I am returning from my web service is
{
"rows": [
{
"id": "100000",
"genre_name": "Action",
"sort_order": "100000"
}, {
"id": "100002",
"genre_name": "Comedy",
"sort_order": "100002"
}, {
"id": "100001",
"genre_name": "Drama",
"sort_order": "100001"
}]
}
My ExtJS code is as below. The loadexception callback is where I have retrieved the JSON and error above from
var genres = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
method: 'POST',
url: 'http://localhost/extjs_training/Demo_WebService/Utility.asmx/GetGenres',
failure: function(response, options){
Ext.get('my_id').dom.innerHTML = 'Load failed: ' + response.status;
}
}),
reader: new Ext.data.JsonReader({
fields: ['id', 'genre_name'],
root: 'rows'
}),
listeners: {
loadexception: function (proxy, options, response, e) {
var result = response.responseText;
Ext.MessageBox.alert('Load failure', e + " ..... " + result);
}
}
});
var loadSuccess = genres.load({
callback: function(r, options, success){
Ext.get('my_id').dom.innerHTML = 'Load status: success=' + success;
}
});
Is the JSON you included above what is actually being returned from the call, or what you are anticipating it should look like? The string you included looks clean, but it looks like you formatted it as well. I'm not sure if the space after "id": is allowed, either. It might not be a big deal, though.
The missing parenthetical typically indicates that something in the JSON is wrong. It could be an extra character before/after the string. Use Firebug to examine what you are getting back, and make sure it is clear of any extra characters.
http://www.sencha.com/forum/showthread.php?10117-Solved-missing-%29-in-parenthetical.
Echoeing two statements was the reason in my case. So check your echoes again.

Categories