I would like to view the json parse objects structure in eclipse. Due to the cross domain issue I am unable to use the console.log() to view the structure in chrome. How else can I view this [object Object] returned structure from Json.Parse ?
Use JSON.stringify(object), it will convert your object to a string(then you can alert, write or whatever you want).
EDIT
If this is the returned value from the request:
'{"Date":"05\/25\/2013","Description":"New2","Details":"New2","ItemRequestedID":"1343","Picture":,255,217],"Status":1,"User":"2120","UserName":"Ind1"}'
It is not yet parsed, so you need to use JSON.parse(string) to parse it to a valid JS Object. Once it is parsed, this will be the structure of your object:
{
"Date": "05\/25\/2013",
"Description": "New2",
"Details": "New2",
"ItemRequestedID": "1343",
"Picture": ,
255,
217], "Status": 1,
"User": "2120",
"UserName": "Ind1"
}
var obj = JSON.parse(result)//where result is the string above and obj is the parsed object with this structure
Then you will be able to access what you need doing obj.Date, obj.Description etc
Related
I have a script in Sheets that is supposed to flatten a JSON post but am having some difficulty getting around the format of the incoming JSON.
Below, I have an example POST which I am trying to parse. Underneath that is my doPost function, with some of the ways I have tried getting the content. doPost uses another function to actually parse the content, but I can't seem to get around what looks like an array formatted JSON?
Here is an example of the POST object:
[
{
"id": "xyz123",
"payload": {
"reference_id": "id_6",
"unit_id": "000111222",
"origin": {
"name": "T-shirt Supply",
"city": "Jiujiang",
"state": "Jiangxi",
"country": "China",
},
"destination": {
"name": "Main Office",
"city": "Surabaya",
"state": "East Java",
"country": "Indonesia",
},
},
"status": "data_received",
"created_at": "2020-01-29T07:41:33.918Z",
"updated_at": "2020-01-29T07:41:33.918Z"
}
]
I have tried in several ways to access the contents, such as payload.reference_id, but for some reason can't find my way into the curled brackets of the JSON object. Here are some of the ways I have tried:
function doPost(e){
var data = e.postData.contents;
// returns JSON formatted [{ "id": "xyz", "payload" : {"reference" : "1", "updated" : true}}]
var data2 = JSON.parse(e.postData.contents);
// returns [object Object]
var data3 = data[0];
// returns [
var data4 = ContentService.createTextOutput(e.postData.contents).setMimeType(ContentService.MimeType.JSON);
// same result as 'data'
return dataX;
}
I have also attempted various workarounds, such as parsing twice, stringify, and more. Any help is greatly appreciated!!!
I think you don't need to parse e.postData.contents, probably it is already an object, not string.
Or if it is string and there is only one element, you can try this:
var data2 = JSON.parse(e.postData.contents.slice(1,-1));
I believe your goal is as follows.
You want to output the object of the 1st element in an array which is your sample value from doPost.
If my understanding is correct, how about the following modification?
Modified script:
function doPost(e){
var data2 = JSON.parse(e.postData.contents);
return ContentService.createTextOutput(JSON.stringify(data2[0])).setMimeType(ContentService.MimeType.JSON);
}
In this modification, it supposes that e.postData.contents is your sample value. Please be careful about this.
When the above-modified script is used, the 1st element of the array is returned.
Note:
When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful about this.
You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
Reference:
createTextOutput()
I'm making an ajax post request to a super simple python function that takes a student's name and spits out a url that corresponds to it. Currently, the Python function passes this back in json and looks like so when console.log(JSON.stringify(response)) is called:
{"readyState":4,"responseText”:”\ {\”studentURL\”: \”https://prepacademy.jackjohnson.com\”} ”,”responseJSON”: {“studentURL”:”https://prepacademy.jackjohnson.com”},”status":200,"statusText":"OK"}
I was wondering how do I take this larger chunk of information and filter it so that I would only get the https://prepacademy.jackjohnson.com part?
response is a JavaScript Object of which you can access the properties using either Dot-notation or bracket-notation, like so:
let response = {
"readyState": 4,
"responseText": "\ {\"studentURL\": \"https://prepacademy.jackjohnson.com\"} ",
"responseJSON": {
"studentURL": "https://prepacademy.jackjohnson.com"
},
"status": 200,
"statusText": "OK"
};
// dot-notation
console.log(response.responseJSON.studentURL)
// bracket-notation (allows for computed paths)
console.log(response["responseJSON"]["studentURL"])
response.responseJSON.studentURL
I have following JSON file:
weather = [
{
"city": "Munich",
"temp": {
"temp_val": "30 deg",
"temp_unit": "C"
},
"speed": {
"speed_val": 7.31,
"speed_unit": "m/s"
}
}
]
I am new to working with JSON files. I want to save this JSON file as weather.json.
But it gives following error:
Expected value at 1:0 pointing to the first line of the file.
You can't have weather = in your JSON file. JSON stands for JavaScript Object Notation so anything other than a JavaScript Object won't work. You also can't have functions in there. Take a look at the JSON official website to see what the format accepts
This should be correct:
[
{
"city": "Munich",
"temp": {
"temp_val": "30 deg",
"temp_unit": "C"
},
"speed": {
"speed_val": 7.31,
"speed_unit": "m/s"
}
}
]
You can then add this line in your javascript once you load the file into a string:
weather = JSON.parse(some_string);
You are manipulating this file as if it was going to be rendered as JavaScript. This should only be plain text, no variable definitions. Just plain key value pairs. If you want to assign variable, set it to a .js file and render it in a browser.
You should look at an example file for JSON and model it. Remember that JSON is just plain text organized in a particular way.
I have a JSON object
{ "ID": "Test", "Data": [ "Line1", "Line2" ] }
And the following JADE Code
h4 {{object.ID}}:
ul
each line in object.Data
li= line
I can display the ID but I cant iterate over the array it errors out on the each line with "Cant Read Property "Data" of undefined,
doing a
p {{object}}
prints the above JSON just fine..
any ideas why this is?
Update: The Object is being passed in from an angular controller.
Looks like it was the angular.
li(ng-repeat='note in object.Data') {{note}}
not the JADE.
I have a JSON formatted text file that I'm using as kind of a database.
I read the file, use JSON.parse to turn it into an object, use delete on an element, then I JSON.stringifythe object and write it back to the file.
However the resulting file has "null" in the place where the object used to be, which isn't proper JSON, so my program will crash the next time the file is parsed. I don't like it.
How can I delete elements from my file without getting "null" where the element used to be?
Here's how I do it:
data = fs.readFileSync("./manifest/test.json");
contents=JSON.parse(data);
//some logic
delete contents.customers[i].files[j];
fs.writeFileSync("./manifest/test.json",JSON.stringify(contents,null,4));
And the resulting file before:
{
"customers": [
{
"customer": "test",
"files": [
{
"name": "test.flv",
"location": "cloudfront.url.com/check.flv"
}
]
}
]
}
And after:
{
"customers": [
{
"customer": "test",
"files": [
null
]
}
]
}
contacts.customers[i].files.splice(j, 1);
The result is still valid JSON though. JSON.parse should succeed. The error may lie elsewhere.