What is the shortest method to convert a JavaScript object to a JSON object? The beneath is my JavaScript object.
{
"body": [
{
"id": "1",
"action": "alert",
"activityGroupNames": "test2"
},
{
"id": "2",
"action": "alert",
"activityGroupNames": "test3"
},
{
"id": "3",
"action": "alert",
"activityGroupNames": "test2"
}
]
}
I making use of an inline code script in Microsoft automate that performs the following in JavaScript:
var threat = workflowContext.actions.Compose.outputs;
var value = Object.values(threat);
return value;
I am supposed to HTTP POST a JSON object in an API request, however, I am submitting a JavaScript object type and am unsure as to how I can change this. Any help would be greatly appreciated! Please let me know if you require any further context.
Edit: The HTTP POST request is failing due to "Object reference not set to an instance of an object"
Javascript provides with built-in method JSON.stringify() It will help you to convert JavaScript object into JSON and pass through HTTP Request
console.log(JSON.stringify({ x: 5, y: 6 }));
// expected output: "{"x":5,"y":6}"
A common use of JSON is to exchange data to/from a web server.
When receiving data from a web server, the data is always a string.
Parse the data with JSON.parse(), and the data becomes a JavaScript object.
const json = '{ "fruit": "pineapple", "fingers": 10 }';
const obj = JSON.parse(json);
console.log(obj.fruit, obj.fingers);
Related
I have JSON object and I want to access "content" which I have highlighted in the s2 snapshot so currently this is the situation "content": "". So if I place some div or some anchor tag in the content something like that "content": " " So How to access this url i.e href . Any help will be highly appreciated.
usually people recieve a json object in some type of request to an API
a json object literal is already a javascript object so you'd just assign it to a variable and access it like you would any js obj
watch out for the nested array as well
var obj = yourExampleJsonObject
console.log(obj.value[0].body.content)
also please don't post code as an image in the future. Cheers!
var obj = {
"key": "value",
"array": [{
"id": "123456",
"body": {
"content": "<tag></tag>"
}
}],
}
console.log(obj.array[0].body.content);
I am trying to parse a JSON from a GET request in JavaScript. I can run it through JSONLint and other tools and it's fine. If I copy/paste the exact result into the JavaScript I can access the values from the object as well.
Here is a shortened example of what the JSON structure looks like:
{
"odata.metadata": "EXAMPLE.com/$metadata#EXAMPLE.results",
"value": [{
"SerialNo_Company": "1",
"Calculated_AsBuilt": "AsBuilt",
"SerialNo_SerialNumber": "1",
"SerialNo_PartNum": "2A",
"JobHead_RevisionNum": "A",
"JobMtl_MtlSeq": 30,
"JobMtl_PartNum": "A123",
"JobMtl_RevisionNum": "A",
"JobMtl_Description": "KEY",
"JobMtl_QtyPer": "1.00000000",
"JobMtl_IUM": "EA",
"JobHead_JobNum": "13",
"JobMtl_IssuedQty": "2.00000000",
"JobMtl_RequiredQty": "2.00000000",
"RowIdent": "7e91b43a-897c-49b4-b68f-307b9ba74832"
}, {
"SerialNo_Company": "1",
"Calculated_AsBuilt": "AsBuilt",
"SerialNo_SerialNumber": "1",
"SerialNo_PartNum": "2A",
"JobHead_RevisionNum": "A",
"JobMtl_MtlSeq": 30,
"JobMtl_PartNum": "A123",
"JobMtl_RevisionNum": "A",
"JobMtl_Description": "KEY",
"JobMtl_QtyPer": "1.00000000",
"JobMtl_IUM": "EA",
"JobHead_JobNum": "14",
"JobMtl_IssuedQty": "2.00000000",
"JobMtl_RequiredQty": "2.00000000",
"RowIdent": "6251fbaf-36d0-4fcf-b65a-9dde70ffb13d"
}]
}
This format isn't entirely correct though, what it actually outputs has a structure like so:
"{\r\n \"odata.metadata\":\"EXAMPLE.com/$metadata#Example.results\",\"value\":[\r\n {\r\n \"SerialNo_Company\":\"1\",\"Calculated_AsBuilt\":\"AsBuilt\",\"SerialNo_SerialNumber\":\"1\",\"SerialNo_PartNum\":\"2A\",\"JobHead_RevisionNum\":\"A\",\"JobMtl_MtlSeq\":30,\"JobMtl_PartNum\":\"A13518N-28-KS\",\"JobMtl_RevisionNum\":\"A\",\"JobMtl_Description\":\"KEY\",\"JobMtl_QtyPer\":\"1.00000000\",\"JobMtl_IUM\":\"EA\",\"JobHead_JobNum\":\"13\",\"JobMtl_IssuedQty\":\"2.00000000\",\"JobMtl_RequiredQty\":\"2.00000000\",\"RowIdent\":\"111531e1-f4da-4d2d-b980-0074227a474e\"\r\n }\r\n ]\r\n}
When I try to query the first structure when hard coded in javascript, for example
object.value[0]
I get the first value and all is good.
The second one does not return a value with the same query. I removed the \r\n and it did not work. Also, JSON.stringify() puts in a bunch of backslashes in the structure and I don't know why.
Thanks for reading if you made it this far, please help.
The solution was to change the output in my node.js app. I was using res.json() and it was double-encoding the response. The output resulted in a javascript readable format from there. Thanks to Heretic Monkey for the answer
I have been trying to get resources using GET Request. You can see the actual Query here:
{ "success": true,"data": [
{
"ID": 123
"Code": "Test",
"Enabled": true,
"Flags": 0,
"Niveau": 0,
"SQL": "SELECT GEMEENTE.GM_ID,\r\n GEMEENTE.GM_POSTNUMMER,\r\n
GEMEENTE.GM_DEELGEMEENTE,\r\n GEMEENTE.GM_LAND_FK,\r\n
LAND.LN_ACTIEF\r\nFROM GEMEENTE\r\n LEFT JOIN LAND ON GEMEENTE.GM_LAND_FK =
LAND.LN_ID\r\nWhere GM_VERANDERDOP >=:ChangedOn",
"Parameters": [
"ChangedOn"
],
"QueryType": 0
}]}
However I need to pass an Argument to the "Parameters" property so i can actually see the response data. I see "ChangedOn" is a the argument array I need to set to a date. I tried this: http://localhost/A/B/server.fcgi/Query?Code=Test&Parameters="2015-07-05T22:16:18Z"
I am working with javascript and vue.
What are we looking at here? Is this the server code of your API. In that case, it looks like you need to supply a ChangedOn parameter. Also, your query string should be url encoded. Try calling...
http://localhost/A/B/server.fcgi/Query?Code=Test&ChangedOn=22015-07-05T22%3A16%3A18Z
I am currently working on a Javascript project where I have to parse tons of data. The project requires that I parse some JSON data and bring specific data into another array. Right now, I am using the JSON.stringify method and I can console.log all of the data that I need. The data looks something like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-85.3865810000125,
33.90171899971196
],
[
-85.38659500025622,
33.9017919996593
],
yes, this is not all that data-it is about 1200 pages long! I only pasted in the top segment of it. All I really need is how to get to the coordinates aspect of it and into an array. So what I am currently doing is this:
var work = JSON.stringify(response, null, 4)
console.log(work);
Which gives me the above response. However, I am not that familiar with JSON.stringify so if I do:
console.log(work.type);
Attempting to see what the value is for type, I get a response of undefined. Now if I try doing this:
var work = JSON.parse(response)
console.log(work);
I get a response of: VM296:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
Thus, I cannot get the data parsed which I want to get to which will be the coordinates data. Any help with using stringify would great help me. I have read a lot about how it turns the data into a string but have not really seen a lot about how to parse it. Do I have to use parse? Thank you for your help!
You dont' have to use stringify or parse for this. The response object you are receiving is already a javascript object so you can reference its properties normally
response.type; // => "FeatureCollection"
If you try and stringify the response it will lose its type property, for instance:
typeof "some string".someProperty; // => undefined
I want to create this parameter file so that I can send it to my web service.
var parms = {
"quiz_id":"120",
"owner_id":"1",
"solver_id":"1",
"answers":
[
{
"answer_text" : "YES",
"question_id" : "1"
},
{
"answer_text" : "NO",
"question_id" : "2"
},
{
"answer_text" : "YES",
"question_id" : "3"
},
{
"answer_text" : "YES",
"question_id" : "4"
},
{
"answer_text" : "YES",
"question_id" : "5"
}
]
};
I am stuck with the contents inside of the answers. I don't know how to create it dynamically.
for serializing Javascript objects to JSON strings either you can use
JSON.stringify(Object);
which is available in most of the latest browers, else you can use ExtJS inbuilt method
Ext.encode(Object);
and for deserializing JSON string you can use JSON.parse(JSONString) or Ext.decode(JSONString)
An easy way to do this is to create your data as a javascript object and then use a Json "stringifier" to turn it into a json string, which can then be passed to your server.
This same problem was answered previously at Serializing an object to JSON
If you use jquery (and I highly recommend it as a very useful tool for all serious javascript programmers), there is a nice plugin that I use for passing json back and forth in Ajax calls. See http://code.google.com/p/jquery-json/
Create an object with array and some objects inside and then look at the Ext.data.proxy.Server.encodeFilters() method.