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);
Related
This is the data displaying in console.log.
{"data":
[
{
"CloserName":null,
"agent_id":"10807",
"AgentName":"TEST",
"SurveyDate":"02/02/2018 02:18:46 AM",
"SurveyName":"Ruth ",
"state":"West Bengal",
"phone":"9836969715",
"status":"Approved",
"verification_progress":"Pending",
"survey_id":"1",
"rejection_remarks":"aa",
"tl_remarks":"Pending"
}
],
"count":1
}
Can anyone help me display a single value (i.e survey_id)? I just want to fetch that survey_id
Here's an example:
var json = {
"data":[
{
"CloserName":null,
"agent_id":"10807",
"AgentName":"TEST",
"SurveyDate":"02/02/2018 02:18:46 AM",
"SurveyName":"Ruth ",
"state":"West Bengal",
"phone":"9836969715",
"status":"Approved",
"verification_progress":"Pending",
"survey_id":"1",
"rejection_remarks":"aa",
"tl_remarks":"Pending"
}
],
"count":1
}
// get first id
var id = json.data[0].survey_id
console.log(id)
// get all ids
var ids = json.data.map(x => x.survey_id)
console.log(ids)
If the JSON is stringified, call JSON.parse(jsonStr) first.
You have to parse the JSON-String into an object. After that you can access the data with default object-identifiers.
const object = JSON.parse('{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}');
console.log(object.data[0].survey_id)
If your JSON data has been stringified (your sample JSON is a valid JSON object, not a string) you would first need to parse it, and then get the IDs (assuming you will have more than one item inside the data array) and log them out. There's a few different ways of achieving this:
const stringified = '{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}';
let parsed = JSON.parse(stringified);
parsed = parsed.data.map(item => item.survey_id);
console.log(parsed);
You can also just loop over the items in the array and log them one by one using a for loop:
const stringified = '{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}';
let parsed = JSON.parse(stringified);
for (let i = 0; i < parsed.data.length; i++) {
console.log(parsed.data[i].survey_id);
}
Or using a for-of:
const stringified = '{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}';
let parsed = JSON.parse(stringified);
for (const item of parsed.data) {
console.log(item.survey_id);
}
According to your current object structure
Your object
var obj = {"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1};
Fetching survey_id
obj.data[0].survey_id
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;
});
[
[
{"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];
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]
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);