Action Script 3.0 : parsing json data - actionscript-3

I want to parse the json data shown in this link in AS3 to obtain the "message" field . I am trying with
as3corelib.swc with no success. The json data seems to be bit different . Please help me to parse this
I am using the below code
var j:Object = JSON.decode(Json_data);
var message:String = j["message"];
trace(message);
Here trace always showing null

There is no message under root object. Probably you are looking for this:
var j:Object = JSON.decode(str);
var data:Array = j["data"];
for (i = 0; i < data.length; i++) {
trace(data[i].message);
}

Related

JSON Query in Google Sheets/Scripts

I am importing data from a JSON file using Google Apps Script and Google Sheets. I have learned the basics on this, but the formatting on the JSON file I am attempting to parse is throwing me off.
What is confusing me is how I would search for information based on "name". Currently I am using this:
function JSONReq(url, xpath){
var res = UrlFetchApp.fetch(url);
var content = res.getContentText();
var json = JSON.parse(content);
var patharray = xpath.split("/");
for(var i = 0; i < patharray.length; i++){
json = json[patharray[i]];
}
return json;
}
I'm a bit lost now to be honest with you.
I want to have a cell where I can type a name that I already know of, then find it in the JSON file and pull the return that information however I decide to do it. I can pull and write to cells, I have the basics down. But I just can't understand how I could search by the name.
That JSON file is an array of objects. To find a specific object with a given "name", you would parse it into an object (which you do already), then iterate through them and check the name parameter:
var myName = "name of thing I want";
var arr = JSON.parse( ... );
for(var i = 0; i < arr.length; ++i) {
var obj = arr[i];
if(obj.name == myName) { // could be done as obj["name"] == ... too
// do stuff with obj
}
}
For your case, you might add an additional argument to your function (i.e. 2nd arg = the object's property, e.g. "name", with the 3rd = the desired value. This will be fine for any simple key-value properties, but would need specific handling for where the value is itself an object (e.g. the "category" field in your specific JSON file).

What is the best way to parse XML data that might contain empty nodes?

I am using the parse XML service to parse some XML date. However sometimes there are child nodes that do not exist then I get an error if I try to getText() of undefined. So I started writing if statements to check every child node for undefined. Is there a better way?
var document = XmlService.parse(rawData);
var root = document.getRootElement();
var entries = root.getChildren();
for (i = 0; i < entries.length; i++) {
var name = entries[i].getChild('name').getText();
var webAddresses = contactData.getChild('web-addresses');
// Checks to see if webAddresses actually exists
if (webAddresses != undefined) {
var webAddress = webAddresses.getChild('web-address');
}
}
See https://developers.google.com/apps-script/reference/xml-service/content. You can use the various asXxx() methods to determine the type of each element.

Inject scope of a function into JSON.parse

I have a bunch of JSON files that I need to parse (in node), but many of the files have things like this:
"_id" : NumberLong(528000021)
Where NumberLong is a function out of scope of the JSON file. When I run JSON.parse I get an error (understandably) that it found an unexpected token. Is there a way to create a function NumberLong and inject it into the scope of the parse?
--EDIT--
Gave #Svabael the answer karma on this one, but for the curious here is how I ended up solving the problem. I created GLOBAL functions for any function appearing in the "JSON", and then created a module then required it.
#!/usr/bin/env node
var fs = require('fs');
GLOBAL.ISODate = function(x){return x};
GLOBAL.NumberLong = function(x){return x};
var source = "./JSONFiles/";
var target = "./JSONModules/";
for(var i=2;i<process.argv.length;i++) {
var fn = process.argv[i];
var sn = source + fn;
var sd = fs.readFileSync(sn,'utf8');
var tn = target + fn.replace('.json','.js');
var td = "module.exports = " + sd;
fs.writeFileSync(tn,td);
var json = require(tn);
//json now has the data
}
To call the script above it is a bash one-liner:
ls JSONFiles | xargs ./json2modules.js
This is not a valid JSON:
{
"_id" : NumberLong(528000021)
}
This is a valid JSON:
{
"_id" : "NumberLong(528000021)"
}
I think that you are trying to parse a javascript object and the error that you have is normal. If this is the case, then you don't need to parse it at all.

How to get CSV data as a string from GmailAttachment?

I'd like to grab gmail attachments that are CSV files then import them into a google sheet.
Here's where I'm stuck - turning the attachment into a string. I think I have a blob to which the method getContentAsString applies, but apparently I still have the type GmailAttachment because I'm getting this error:
TypeError: Cannot find function getContentAsString in object GmailAttachment.
here's the relevant code:
//************** get the attachments ***************************************
var attachments = [];
var files = [];
for (var i = 0; i < 4; i ++) {
attachments = messages[i].getAttachments();
for (var k = 0; k < attachments.length; k = k+2) { //2 attachments per message, but I only want the 1st one
j = k/2;
files[j] = attachments[k].copyBlob();
Logger.log('Message "%s" contains the attachment "%s" (%s bytes)',
messages[i].getSubject(), files[j].getName(), files[j].getSize());
}
}
var csvFile = "";
for (var i = 0; i < files.length; i++) {
csvFile = files[i].getContentAsString();
}
why is .copyBlob() not returning a blob, but in this case a GmailAttachemnt, and how can I fix it?
is the problem here:
files[j] = attachments[k].copyBlob();
?
by the way I also tried getBlob() instead of copyBlob() and it returned a type error at this line above.
using copyBlob() I get the typeError at this line:
csvFile = files[i].getContentAsString();
Thanks for your help!
Inorder to get the string value of a blob you should use: getDataAsString() method in Blob class. The .getContentAsString() method is from the DocsList Service which is deprecated.
You could also use the getDataAsString() method from the GmailAttachment class itself. Hope that helps!

How we can get value of json code in jquery

I send data using ajax and return json code.How can I get each Name key's value. Below is my code :-
[{"Name":"Calendar","EmailTemplates":[],
"Href":"http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/1"},
{"Name":"Products","EmailTemplates":[],
"Href":"http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/2"},
{"Name":"Personal Events","EmailTemplates":[],
"Href":"http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/3"},
{"Name":"Health Tips","EmailTemplates":[],"Href":"http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/4"},
{"Name":"Financial Tips ","EmailTemplates":[],"Href":"http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/5"}]
I want this output :
calender
Procudt
Try this,
var x = eval(YourJsonInput);
for(var i=0; i<x.length; i++)
{
var name = x[i].Name; //Here you get the name
var link = x[i].Href; //Here you get the href
}