convert json to javascript object for datables - javascript

I want to convert json into a javascript object but i am unable to do so,since i am new.
{"campaigns":{"campDetails":[{"campaign_id":"1012","campaign_name":"RP - Axe Sample (Submit)"}]}}
into
"columns": [
{ "data": {} },
{ "data": "hr.position" },
{ "data": "contact.0" },
{ "data": "contact.1" },
{ "data": "hr.start_date" },
{ "data": "hr.salary" }
]
javascript object format the above is the javasctipt object to another another json. Just avoid the data I needed to know how you parse example
"columns" : [{data:campaigndetails{ _:campaigns }}]
etc is want the out put be like
columns:[
{data:1012}.
{data:RP - Axe Sample (Submit)}
]
but i dont really know the parameters

Check out http://api.jquery.com/jquery.parsejson/ - That has a good API for your task.
Else, check Converting a string to JSON object - Somewhat similar to your problem.

You can use eval to convert any string into json
var str = {"campaigns":{"campDetails":[{"campaign_id":"1012","campaign_name":"RP - Axe Sample (Submit)"}]}};
var obj = eval(str);

Related

how do you format a json parameter object such that it calls correctly in a template?

I'm having some trouble formatting an object in a json parameters file. All values are strings.
"BigObject": {
"value": {
"subobject1":["value": {
"data1",
"data2"
}],
"subobject2":["value": {
"data1",
"data2"
}],
"subobject3":["value": {
"data1",
"data2"
}],
}
}
If this is referenced in a json arm template, and I need data1 from subobject1, would it be like the following?
"[parameters('BigObject').subobject1[0]]"
EDIT: "bigobject" is an object, but subobject is expected to be an array of values.
Like mentioning in here
"[parameters('BigObject').value.subobject1[0]]"
if you want data, something like below (it depends on your JSON Structure)
"[parameters('BigObject').value.subobject1[0].value.data1]"

How access nested JSON node after converting from SOAP?

Using node.js(javascript) how do I access the GetDataResult node in this JSON data that has been converted from SOAP data.
{
"s:Envelope": {
"$": {
"xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/"
},
"s:Body": [{
"GetDataResponse": [{
"$": {
"xmlns": "http://tempuri.org/"
},
"GetDataResult": ["You entered: TEST"]
}]
}]
}
}
Test using nodejs interactive mode :
$ node
> var x = {
... "s:Envelope": {
..... "$": {
....... "xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/"
....... },
..... "s:Body": [{
....... "GetDataResponse": [{
......... "$": {
........... "xmlns": "http://tempuri.org/"
........... },
......... "GetDataResult": ["You entered: TEST"]
......... }]
....... }]
..... }
... }
undefined
> console.log(x["s:Envelope"]["s:Body"][0]["GetDataResponse"][0]["GetDataResult"][0])
Output :
'You entered: TEST'
Explanations :
I try to elaborate a bit from comments below. There is no container, I try to explain :
You have to think json like what it is : an object or a data structure.
In python, we would say it's a dict, in perl a hash table etc... Globally, it's all about associative array
So when you see in JSON :
"key" : { "value" }
it's an associative array
If instead you see
"key": [
{ "key1": "foo" },
{ "key2": "bar" },
{ "key3": "base" }
]
It's an array of hashes or array of associative arrays.
When you access a simple associative array without spaces or odd characters, you can (in js do :
variable.key
In your case, you have odd character : in the key name, so x.s:Envelope wouldn't work. Instead we write: x['s:Envelope'].
And as far as you have arrays of associative arrays inside [], you have to tell js which array number you need to fetch. It's arrays with only one associative array, so it's simple, we go deeper in the data structure by passing array number, that's what we've done with
x['s:Envelope']["s:Body"][0]
^
|

Converting JS array UNIX date integers into JS Date objects

Is there a way to convert UNIX epoch integers (1402079444, etc) in an array into JavaScript Date objects (Date.UTC(2014, 9, 14), etc) using jQuery?
I'm trying to pass a large JSON array generated by PHP to Highmaps.JS, which almost works great however Highmaps expects a Date object and Date objects aren't valid JSON, so I can't generate them with PHP.
jsFiddle of my current setup here: http://jsfiddle.net/dwgLtscm/2/
(The x-axis isn't displaying dates properly because the data isn't in the proper date format).
[{
"name": "Dissolved Oxygen",
"data": [
[1402079444,9]
]
},
{
"name": "Temperature (Water)",
"data": [
[1401291099,9],
[1401862547,12]
]
},
{
"name": "Temperature (Air)",
"data": [
[1401291099,13],
[1401862547,19]
]
},
]
Given the Json object above, I'd try:
array.forEach(function (val) {
val.data = val.data.map(function (datum) {
return [ new Date(datum[0] * 1000), datum[1] ];
}
}
Unless I'm reading it wrong (I'm assuming data[0] is the UTC value).
(Edited based on feedback below, thanks all!)

Javascript JSON syntax

HI I am trying to create a JSON file in which I want to store some data for different files. The problem is I cannot figure the correct syntax. Here is what I have so far:
var object = {
"id-1" :[
{
"type":"Corporate Website",
"tech":"HTML" ,"CSS" , "Javascript/jQuery"
}
],
"id-2" :[
]
}
I seem to be getting an error at "tech".If that is not corect how can I enumarate multiple elements?I am sorry for the noob question I have been using javascript for a short period of time and I am still very confused with the language.
{
"id-1": [
{
"type": "Corporate Website",
"tech": [
"HTML",
"CSS",
"Javascript/jQuery"
]
}
],
"id-2": []
}
Note the array like syntax for "tech".
Tech should be an array (enclosed in square brackets):
"tech": ["HTML", "CSS", "Javascript/jQuery"]
Source:
An array is an ordered collection of values. An array begins with [
(left bracket) and ends with ] (right bracket). Values are separated
by , (comma).
http://www.json.org/

Trouble parsing multi-level JSON

I have a chunk of JSON which looks something like:
{
"map": [
[
"zimbraFeatureCalendarUpsellEnabled",
"FALSE"
],
[
"zimbraPrefCalendarDayHourStart",
"8"
],
[
"zimbraFeatureOptionsEnabled",
"TRUE"
],
[
"zimbraAttachmentsViewInHtmlOnly",
"FALSE"
]
]
}
(and so on; there's 200+ entries)
I need to be able to pick out individual key/value pairs from the JSON response, either with jQuery or plain old Javascript. I haven't been able to figure out how to address a specific key, though. Any ideas?
What you've described is a single level object, with a whole bunch of nested arrays so accessing will be
myObject.map[entryNumber][0 or 1] // 0 == key, 1 == value
You probably want something akin to this (unless you're working with existing API or some such):
{
"map": {
"zimbraFeatureCalendarUpsellEnabled": "FALSE",
"zimbraPrefCalendarDayHourStart": "8",
...
}
}
Instead of using arrays, you could use an object:
{
map : {
"zimbraFeatureCalendarUpsellEnabled" : "FALSE",
"zimbraPrefCalendarDayHourStart" : "8",
"zimbraFeatureOptionsEnabled" : "TRUE",
"zimbraAttachmentsViewInHtmlOnly" : "FALSE"
}
}
and then to access it:
myJSONObject.map.zimbraFeatureCalendarUpsellEnabled;

Categories