I am trying to use the Kairos API for Facial Recognition in my air app. After i send the image, kairos returns the following JSON:
{"images":[{"transaction":{"status":"failure","topLeftX":106,"topLeftY":126,"gallery_name":"Faces","eyeDistance":42,"height":98,"width":98,"face_id":1,"quality":-1.53973,"message":"No match found"}}],"uploaded_image_url":"https:\/\/kairos-east-id-images.s3.amazonaws.com\/prod\/c6d565457\/recognize\/Faces\/d2b1142f2134232349ewer8acb825c87e909f299ab1_5a234XXXXXX.jpg?X-Amz-Content-Sha246=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXXXXXXXXX-east-1%2Fs3%2XXXX_request&X-Amz-Date=2017122rrtdfg158Z&X-Amz-SignedHeaders=host&X-Amz-Expires=XXXX&X-Amz-Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}
I need to access the values of "topLeftX", "topLeftY" and "message" but no matter what i try it doesn't seem to work. I have been searching around for about an hour. I have tried both native JSON parser and ascorelib JSON parser.
I tried the following:
var rawData:Object = JSON.decode(e.target.data);
for ( var object:Object in rawData ){
trace(object.transaction);
}
I get this error:
Property transaction not found on String and there is no default value.
I tried with different property names but I get the same error. I have tried sever other methods as well. Such as,
rawData["transaction"][0]["topLeftX"]
It doesnt work.
Any help is greatly appreciated.
I do not have a decode function on JSON. Just a JSON.parse and a JSON.stringify however what should work for you is this.
var jsonObj = JSON.parse(yourdata); // or JSON.decode if you AS version is older
for ( var i = 0 ; i < jsonObj.images.length ; i++ ){
trace(jsonObj.images[i].transaction.status);
}
Related
I have this response from an API:
payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'}
how can i select the pix_expiration_date key??? I have tried:
payment_object['acquirer_response']['pix_expiration_date']
it doesnt work and it returns to me:
TypeError: string indices must be integers
Since you’re looking for json
Here’s a code you can start with, keep the good work :)
import json
#importing json
payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'}
###
dic=json.loads(payment_object["acquirer_response"])
#loading the object
dic['pix_expiration_date']
#it will return 2022-04-28T13:46:01.000Z
Are you the owner of the api? If yes:
Edit the response, because it’s sending an string not an object/dictionary
If No:
Convert the string to an dictionary, like if you using python try json library
Your json is not valid. I don't know what language are you using, this code works for javascript, you can easily translate it to another language
var payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'};
var s=JSON.stringify(payment_object).replaceAll("\\","").replaceAll("\"{","{").replaceAll("}\"","}");
var paymentObject=JSON.parse(s);
console.log(paymentObject['acquirer_response']['pix_expiration_date']); // 2022-04-28T13:46:01.000Z
I have a function in AWS Lambda in which I retrieve an unparsed JSON object, I parse it, and then access its values.
if(data.Payload){
var parsedData = JSON.parse(data.Payload)
console.log("PAYLOAD --> " + parsedData);
console.log("GROUPNAME --> " + parseData.groupName);
...
When I log to the console the parsedData variable, it seems like the parsing was successful:
PAYLOAD --> {"groupName":"Hello!","membersCount":1,"searchField":"hello!"}
The issue arises when I try to access the fields in the JSON as I keep getting undefined:
GROUPNAME --> undefined
NOTE:
If I copy and paste the JSON object
{"groupName":"Hello!","membersCount":1,"searchField":"hello!"}
into a variable on the Chrome debugging console
var parsedData = {"groupName":"Hello!","membersCount":1,"searchField":"hello!"}
I am able to access the properties of the object as I am trying to do in the AWS Lambda function.
parsedData.groupName prints "Hello!"
Edit - temporary solution
The parsedData variable contains a String with a JSON, so the JSON object inside the "" I am not quite sure why. The temporary fix was to double parse the variable but that just seems wrong.
if(data.Payload){
var parsedData = JSON.parse(data.Payload);
var doubleParsed = JSON.parse(parsedData);
if(doubleParsed.groupName !== undefined) {
console.log(doubleParsed.groupName);
}
}
If that is a copy paste of your code, in your 2nd console.log you are using parseData NOT parsedData missing a D.
EDIT Just adding as answer what I wrote in comments.
It seems parsedData was not being parsed correctly, for some reason JSON.parse is not working, I think some information about data.Payload is needed to know what it exactly returns.
Yet the next code seems to solve it, but I would honestly need further explanation as why it needs to be done twice:
var parsedData = JSON.parse(JSON.parse(data.Payload));
I am working on a WP8 app. In this I need to connect to web services whose results will be some JSON. I was trying to extract some data from the result that the web service provide. I was able to extract from the initial JSON response. But I need to get some data from the value of one such key . SO I tried to generate another Json object from it. But I m stuck. please help.Please find my example code below(I am using Newtonsoft.JSon).
private void messages_buttons_Click(object sender, RoutedEventArgs e)
{
var str = "{'status': '0', 'result': '%7B%22campaign_id%22%3A%221%22%2C%22tfn%22%3A%2218773374136%22%2C%22campaign_code%22%3A%22PJC%22%2C%22ad_id%22%3A%221%22%2C%22qr_url%22%3A%22http%3A%5C%2F%5C%2F1d1.us%5C%2FPJC%5C%2F%22%2C%22campaign_name%22%3A%22PJ+Test+Campaign%22%2C%22is_active%22%3A%221%22%2C%22expire_on%22%3A%222021-05-05+00%3A00%3A00%22%2C%22start_on%22%3A%222021-05-05+00%3A00%3A00%22%2C%22alias%22%3A%22%22%2C%22icon_image_url%22%3A%22products%5C%2Fpjc%5C%2Fpjc3.jpg%22%2C%22fb_page_url%22%3A%22https%3A%5C%2F%5C%2Fwww.facebook.com%5C%2FJackLaLannePowerJuicerssfb%22%2C%22video_url%22%3A%22http%3A%5C%2F%5C%2Fyoutube.com%5C%2Fembed%5C%2FyZPedpRA9r0%3Fshowinfo%3D0%26autoplay%3D1%26loop%3D1%26playlist%3DyZPedpRA9r0%22%2C%22url%22%3A%22https%3A%5C%2F%5C%2Fwww.facebook.com'}";
JObject ne = JObject.Parse(str);
var x= (ne.GetValue("result")).ToString();
var z = x.Replace("%", "");
JObject newest = JObject.Parse(z);
var y = newest.GetValue("campaign_id");
MessageBox.Show(y.ToString());
}
I get an exception at "JObject newest = JObject.Parse(z);" with the message
Unexpected character encountered while parsing number: m. Path '', line 1, position 6.
Am I doing it entirely wrong?
On a general note: can I convert a value from one Json to a another JSOn Itself? i.e if the value of one json key is a string with some key value pairs, can i make a json object on that string?
You can't actually just remove the % chars to get a valid value. You need to decode the string.
If you use this:
HttpUtility.UrlDecode(x);
You'll find your "result" is actually invalid JSON:
{"campaign_id":"1","tfn":"18773374136","campaign_code":"PJC","ad_id":"1","qr_url":"http://1d1.us/PJC/","campaign_name":"PJ
Test Campaign","is_active":"1","expire_on":"2021-05-05
00:00:00","start_on":"2021-05-05
00:00:00","alias":"","icon_image_url":"products/pjc/pjc3.jpg","fb_page_url":"https://www.facebook.com/JackLaLannePowerJuicerssfb","video_url":"http://youtube.com/embed/yZPedpRA9r0?showinfo=0&autoplay=1&loop=1&playlist=yZPedpRA9r0","url":"https://www.facebook.com
So hacking the value to make it valid JSON might work for you, by adding the missing "} at the end should turn your value in to valid JSON and allow you to parse it.
JObject newest = JObject.Parse(x + "\"}");
var y = newest.GetValue("campaign_id");
It doesn't appear that z is a valid json object at this point. It is only the value of result. Try something like JObject.Parse("'result':" + z);
I am running into an error parsing a json string with JSON.parse(...)
var str:String= '[{"AA":"A1", "BB":"32"}, {"AA":"A2", "BB":"12"}, {"AA":"A3", "BB":"14"}]';
var propertySets:Object = JSON.parse(str);
I can tell that I am getting the syntax wrong with constructing the JSON string but I have tried quite a few things before giving up. Any help with how to deal with collections would be great.
The following simple case works for me
var str:String= '{"test":"line1"}';
var propertySets:Object = JSON.parse(str);
Thank you
The syntax in the JSON string is correct. The way it is formatted will return an Array instance from JSON.parse(). The following code works for me:
var str:String = '[{"AA":"A1", "BB":"32"}, {"AA":"A2", "BB":"12"}, {"AA":"A3", "BB":"14"}]';
var propertySets:Array = JSON.parse(str) as Array;
trace(propertySets[0].AA); // prints "A1"
trace(propertySets[0].BB); // prints "32"
Shot in the dark since I don't know actionscript, but try wrapping the array in an object
var str:String= '{"objectArray":[{"AA":"A1", "BB":"32"}, {"AA":"A2", "BB":"12"}, {"AA":"A3","BB":"14"}]}';
this is my json format
({"message":{"success":true,"result":[{"lead_no":"LEA13","lastname":"Developer","firstname":"PHP","company":"Dummies","email":"nandhajj#gmail.com","id":"10x132"},{"lead_no":"LEA14","lastname":"Venu","firstname":"Yatagiri","company":"Rsalesarm","email":"veve#jajs.com","id":"10x133"},{"lead_no":"LEA4","lastname":"Jones","firstname":"Barbara","company":"Vtigercrm inc","email":"barbara_jones#company.com","id":"10x35"},{"lead_no":"LEA1","lastname":"Smith","firstname":"Mary","company":"Vtiger","email":"mary_smith#company.com","id":"10x32"}]}})
i am trying to retrieve the whole json result values using the following snippet
if (xmlHttp.readyState==4)
{
alert(xmlHttp.status);
if(xmlHttp.status==200)
{
alert("hi");
var jsondata=eval("("+xmlHttp.responseText+")") //retrieve result as an JavaScript object
jsonOutput=jsondata.result;
alert(jsonOutput);
InitializeLeadStorage()
}
}
my alert (hi ) is displayed but the alert(jsonOutput); is undefined , please help me if you could find any mistake
jsonOutput = jsondata.message.result;
result lives on message - it is not a top-level item in the JSON. With things like this, console.log() the JSON and you can check the path to the bit you want.
Also
your variable is global
there are better ways of parsing your JSON. If you don't care about old IEs, you can use the ECMA5 JSON.parse(), else use jQuery or another third-party utility for this