Parsing JSON in Google Scripts - json

I am trying to parse a JSON object in Google Scripts but I can't seem to get it to work.
The JSON I am trying to parse looks like this
{
"keywords":[
{
"c":0.015165274822976534,
"monthly":[ ],
"kw":"handstand",
"n":60500,
"sb":0.3
}
],
"not_found":[
]
}
I can't seem to access any of the values within the object. If I use JSON.parse() it seems to create a non-JSON object.
var response = '{"keywords":[{"c":0.015165274822976534,"monthly":[
],"kw":"handstand","n":60500,"sb":0.3}],"not_found":[]}'
var obj = JSON.parse(response);
Logger.log(obj);
Returns
{keywords=[{c=0.015165274822976534, monthly=[], kw=handstand, n=60500, sb=0.3}], not_found=[]}
This doesn't validate.
If I try to work with that anyways this happens
var response = '{"keywords":[{"c":0.015165274822976534,"monthly":[
],"kw":"handstand","n":60500,"sb":0.3}],"not_found":[]}'
var obj = JSON.parse(response);
Logger.log(obj.keywords.n)
Returns undefined
If I don't use JSON.parse and just work with the original response object I get the following:
var response = '{"keywords":[{"c":0.015165274822976534,"monthly":[ ],"kw":"handstand","n":60500,"sb":0.3}],"not_found":[]}'
Logger.log(response.keywords);
I get undefined
I'm not really sure where I am going wrong here. Any help is appreciated. Thanks!

Related

Issue returning the nested object in JSON

I am using the following function code to try and return the "sportId" but am not getting anything.
function (err, response, body) {
// parse the body as JSON
var parsedBody = JSON.parse(body);
var apiResults = parsedBody.apiResults;
var sportId = apiResults.sportId;
res.json(sportId);
});
Below is the result when I return just parsedBody.
{"status":"OK","recordCount":1,"startTimestamp":"2017-05-09T21:01:17.9462907Z","endTimestamp":"2017-05-09T21:01:18.1533299Z","timeTaken":0.20703919999999998,"apiResults":[{"sportId":2,"name":"Baseball","league":{"leagueId":7,"name":"Major League Baseball" ...
It also works when I return apiResults, just not for some reason when I try to get sportId. I am new to node.js and cannot figure out why this isn't working.
apiResults is an array of objects. sportId is in that object so you need to use the loop to traverse the sportId
If you want to retrieve the first one then use
var sportId = apiResults[0].sportId

nodejs object.property undefined error on parsing json

I am using nodejs 0.10.32/33 but with either of them i am getting undefined exception on accessing the property of a json object. My test class looks like below:
var jvar = '{"name":"sumit","age":"33"}';
var stdata = JSON.stringify(jvar);
var sdata = JSON.parse(stdata);
console.log(sdata);
console.log(sdata.name);
and output from the above code is:
{"name":"sumit","age":"33"}
undefined
I am unable to get what I am missing here.
you need to parse the string as JSON, because its already string, so no need to use JSON.stringify(), use only JSON.parse instead, as:
var jvar = '{"name":"sumit","age":"33"}';
var stdata = JSON.parse(jvar);
console.log( stdata.name ); //gives sumit
stringify is to convert a json object to string. not vice versa.
var jvar = {"name":"sumit","age":"33"};
var stdata = JSON.stringify(jvar);
var sdata = JSON.parse(stdata);
console.log(sdata);
console.log(sdata.name);

$resource : $save does not work

I have a json file called data.json containing some data:
[
{
"name" : "toto"
}
]
And the script I wrote to manage it :
var Data = $resource("data.json", {},
{
query: {method:'GET',isArray:true}
});
var data = Data.query(function()
{
var d = data[0];
d.name = "Titi";
d.$save();
});
Everything work before I call $save() on my object. I have this error:
[11:30:24.962] "Error: [$resource:badcfg] Error in resource configuration.
Expected response to contain an object but got an array
I don't really know the problem. I have already read many examples and documentations but this does not seem clearer to me.
Just guessing to help quickly here... You may have several issues, var d = data[0]. data should be from an argument/parameter of the function not the var data which is null until the return result from Data.query changes it. $save should also likely be on the $resource object, not the object in the array. Not an angular person, but guessing that might be the case.

Convert JSON object to array?

I have this on my console on firebug,
[Object { fa_id="1167535", f_id="1000", loc_type="6", more...}, Object { fa_id="1167535", f_id="1000", loc_type="6", more...}]
it is data from the server side. Now, how would I convert this into array such that this data can be used on another file. I tried JSON.parse and jQuery.parseJSON but both did not work.
That isn't JSON it's a Javascript array of objects, not a string. My guess is that you've received this from a jQuery ajax call and you had the dataType : 'json' set so that jQuery has automatically parsed the JSON into this array.
To send it to a PHP script you can convert it back to JSON using:
var myString = JSON.stringify(data);
and then fire off an ajax call to the PHP script with that as the POST data:
var myString = JSON.stringify(data);
$.post('page.php', { data : myString }, function(){
console.log( "sent" );
});
In PHP you can decode it using:
$data = json_decode($_POST['data']); // <-- or whatever your post variable is named
foreach($data as $obj)
{
echo $obj->fa_id;
}
if you want to get an php array use this
http://php.net/manual/en/function.json-decode.php
The string you provided is not valid JSON.
[Object { fa_id="1167535", f_id="1000", loc_type="6", more...},
Object { fa_id="1167535", f_id="1000", loc_type="6", more...}]
In particular, the "Object" and "more..." strings cannot be interpreted by a JSON parser.
Assuming the object you are inspecting is a variable named foo:
console.log(JSON.stringify(foo));
Should print a valid JSON representation of your object to the Javascript console.

Parsing JSON data in actionscript 3

I have a JSON response coming up as
[{"serial": "1", "name": "ABC" }]
I am using com.adobe.serialization.JSON class of as3corelib in actionscript. This class works perfectly in other formats of json, but in this case, after doing a JSON.decode to the object I am unable to get the value as obj.name
If I remove the square brackets I am getting a parsing error.
How can I parse the json in this case, I need to get the value of the key "name". Thanks.
Technically to parse all the objects you'll need
// serverResponse answer from the server [String]
var jsonResponse:Object = JSON.parse(serverResponse);
for ( var object:Object in jsonResponse ){
trace(object.name);
}
Well as you edited this is using : ActionScript 3 JSON and it's not just for air, you still can use it
And using AS3CoreLib, here is an example showing how to handle the JSON, so it look like this :
var rawData:String = String(event.result);
var arr:Array = (JSON.decode(rawData) as Array);
for (var i:int = 0; i < arr.length; i++){
trace(arr[i].name);
}
Hope this helps, Cheers !!
You are parsing array of json objects. You need to fetch the value like obj[0].name.
Hope it helps.

Resources