var data = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';
Here I have posted my JSON data. I want to get all the defaultValue in JSON/Array format. My output should be like-
defaultValues:['Size30','Black','Low']
How to manage that in the foreach loop?
my code :
var otherSelectedOption;
angular.forEach(data, function(optionValue, optionKey) {
if (optionValue.defaultValue.text) {
otherSelectedOption = (optionValue.defaultValue.text);
}
selectedOption = {defaultValues: otherSelectedOption};
console.log(selectedOption);
});
Your JSON is not valid, since objects are not separated by comma ,
Suppose this is the JSON
var obj = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';
try
var arr = JSON.parse(obj).map( function(item){
return item.defaultValue;
});
Related
[
[
{"path":"path2","value":"kkk"},
{"path":"path0","value":"uuu"},
{"path":"path1","value":"ppp"}
]
]
I get above result from for my manipulation, But I need it as follows.
["path":"path2","value":"kkk"],
["path":"path0","value":"uuu"],
["path":"path1","value":"ppp"]
Here is my code:
$scope.sharePaths[d.id] = []
d.conf.paths = []
$scope.sharePaths[d.id][index] = []
commaPath = 'kkk,uuu,ppp'
a = commaPath.split(',')
for key of a
value = a[key]
$scope.sharePaths[d.id][index].push {'path':'path'+key, 'value':a[key]}
d.conf.paths.push {'path':'path'+key, 'value':a[key]}
Just use the first element of your array. The variable data is already formatted correctly. The new format you want is not valid JSON.
var data = [[{"path":"path2","value":"kkk"},{"path":"path0","value":"uuu"},{"path":"path1","value":"ppp"}]];
data = data[0];
I want to parse the following json:
{"key_410441":{"hashId":"hash123","tube_id":"4accdefk31"}}
Where key_410441 is the entry's name representing the object's value, and the following array is the object's data.
How can I retrieve it's value?
function defined(json) {
for (var i in json) {
var objId = json[i]. ????
}
}
Like Robo Robok said, use Object.keys(object)
if your json look like {"key_410441":{"hashId":"hash123","tube_id":"4accdefk31"}}
function defined(json) {
var hashId = json[Object.keys(json)[0]].hashId
var tube_id = json[Object.keys(json)[0]].tube_id
}
}
you can use shortcut json[Object.keys(json)] because you have olny one object
key_410441
Object keys are returned in form of an array by Object.keys(object)
I suppose you are using jquery and ajax to get a json from an external file. Then the piece of code would be:-
$.getJSON("aa.json", function(data) {
var obj = Object.keys(data),
json = data[obj];
for(var s in json) {
console.log(json[s]);
}
});
Input of Json Object:
{"data":[
{"itemID":100,"Testcase1":"aaa","status":"Active"},
{"itemID":100,"Testcase1":"bbb","status":"No"},
{"itemID":100,"Testcase1":"ccc","status":"Active"},
{"itemID":101,"Testcase1":"zzz","status":"Active"}
]}
Expected Output of Json Object:
[{
"itemID":"100",
"Testcase1":"aaa",
"Testcase1":"bbb",
"Testcase1":"ccc",
"status":"Active",
"status":"No",
"status":"Active"
},
{
"itemID":"101",
"Testcase1":"zzz",
"status":"Active"
}]
anybody help me?
may be this helps you. This is not the correct way. Because duplicate key is not allowed in object. whatever as it your request. I have create a string of your required format.
please checkit FIDDLE
var dataVal={"data":[
{"itemID":100,"Testcase1":"aaa","status":"Active"},
{"itemID":100,"Testcase1":"bbb","status":"No"},
{"itemID":100,"Testcase1":"ccc","status":"Active"},
{"itemID":101,"Testcase1":"zzz","status":"Active"}
]};
var arrayVal = dataVal.data;
var DistinctID=[];
var resultStr="";
for (var elements in arrayVal){
if($.inArray(arrayVal[elements].itemID, DistinctID) == -1){
DistinctID.push(arrayVal[elements].itemID)
}
}
for (var items in DistinctID){
resultStr +='{"itemID":"'+DistinctID[items]+'",';
for (var elements in arrayVal){
if(arrayVal[elements].itemID==DistinctID[items]){
resultStr +='"Testcase1":"'+arrayVal[elements].Testcase1+'",';
resultStr +='"status":"'+arrayVal[elements].status+'",';
}
}
resultStr = resultStr.substring(0, resultStr.length - 1);
resultStr= resultStr+'},';
}
resultStr = resultStr.substring(0, resultStr.length - 1);
resultStr='['+resultStr+']';
alert((resultStr));
Assuming your "JSON Object" is a string, you can call JSON.parse() on it:
var data = '{"data":[{ ... }]}';
var parsed = JSON.parse(data);
This will give you data in the following structure:
Your expected output isn't a valid object. Object keys must be unique. An object cannot have three Testcase1 keys or three status keys.
You just have to extract objects from data object ?
Ok then, a pure JS solution :
var obj = {"data":[{"itemID":100,"Testcase1":"aaa","status":"Active"},
{"itemID":100,"Testcase1":"bbb","status":"No"},{"itemID":100,"Testcase1":"ccc","status":"Active"},
{"itemID":101,"Testcase1":"zzz","status":"Active"}]};
var newArray = [];
for( var i = 0; i < obj['data'].length; i++) {
newArray.push(obj['data'][i]);
}
And a live demo of that : http://jsfiddle.net/seLwmbyp/1/ (see the console for the result) > [Object, Object, Object, Object]
My JSON data are
[
{"month":"May-2013","total_sales":"1369250"},
{"month":"June-2013","total_sales":"4328119"},
{"month":"July-2013","total_sales":"4636663"},
{"month":"August-2013","total_sales":"4754047"},
{"month":"September-2013","total_sales":"5014683"}
]
How can i craete an array in javascript from following data?
try this one
var json_data = [{"month":"May-2013","total_sales":"1369250"},{"month":"June-2013","total_sales":"4328119"},{"month":"July-2013","total_sales":"4636663"},{"month":"August-2013","total_sales":"4754047"},{"month":"September-2013","total_sales":"5014683"}];
var result = [];
for(var i in json_data)
result.push([i, json_data [i]]);
var data = '[{"month":"May-2013","total_sales":"1369250"},
{"month":"June-2013","total_sales":"4328119"},
{"month":"July-2013","total_sales":"4636663"},
{"month":"August-2013","total_sales":"4754047"},
{"month":"September-2013","total_sales":"5014683"}]'
var json = JSON.parse(data);
console.log(json);
You basically have the array object in JSON
Check http://www.mkyong.com/javascript/how-to-access-json-object-in-javascript/ also.
And to convert it back to json..
var myJsonString = JSON.stringify(yourArray);
I need only the values .how to get value from these json
-wafer,
-Compound Chocolate,
-Praline Chcolate
etc.....
In this a main problem is "subcategory" and its value is taken from database its change according to database
[{"subcategory":"Wafer"},{"subcategory":"Compound Chocolate"},{"subcategory":"Praline Chcolate"},{"subcategory":"Cookies"},{"subcategory":"Toffee"},{"subcategory":"Eclair"},{"subcategory":"Fruit Chews"}]
each json has a value like subcategory
Fiddle: http://jsfiddle.net/KGms7/
var str = '[{"subcategory":"Wafer"},{"subcategory":"Compound Chocolate"},{"subcategory":"Praline Chcolate"},{"subcategory":"Cookies"},{"subcategory":"Toffee"},{"subcategory":"Eclair"},{"subcategory":"Fruit Chews"}]';
var jsn = JSON.parse(str);
var arr = [];
for(var i in jsn) {
arr.push(jsn[i].subcategory);
}
alert(arr);