How could i parse this type of json data, getting in "results" to fetch single values like zipcode, state etc
{
"row": [
{
"id": "5",
"name": "test",
"email": "test#test.com",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"}
]
}
first, you need to parse that string with JSON.parse
var myJson = JSON.parse(the_raw_data_string);
it ends up into an object like this:
var myJson = {
"row": [
{
"id": "5",
"name": "test",
"email": "test#test.com",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"}
]
}
accessing the items:
myJson.row[0].id
myJson.row[0].name
myJson.row[0].street
//and so on...
you can take the json result to a var like follows
var json = {
"row": [
{
"id": "5",
"name": "test",
"email": "test#test.com",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"}
]
}
then get the result to another
var result = json.row;
then you can iterate through the result
for (var i = 0; i < result.length; i++) {
var object = result[i];
for (property in object) {
var value = object[property];
alert(property + "=" + value); // This alerts "id=5", etc..
}
}
hope this will help you
Again here jQuery is your good friend
I have posted a sample using jsfiddle with multiple records in your data row
$(document).ready(function () {
var result = {
"row": [
{
"id": "5",
"name": "test",
"email": "test#test.com",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"
},
{
"id": "10",
"name": "test2",
"email": "test2#test.com",
"street": "mystreet2",
"city": "mycity2",
"state": "mystate2",
"zipcode": "7891011",
"myimage": "image.gif"
}
]
};
var oE = $("#output");
$.each(result.row, function(index, value) {
//- extract target value like zipCode
oE.append($("<li></li>").text(value.zipcode));
});
});
Hope this helps.
If the json data is raw then use json.parse.
After that to loop the multi dimensional json data.
data = {"employees":[
{ "firstName":"Anushka", "lastName":"shetty" },
{ "firstName":"Shreya", "lastName":"Saran" },
{ "firstName":"Kajal", "lastName":"Agarwal" }
]};
for (var key in data.employees) {
alert(data.employees[key].firstName) //alert Anushka, Shreya, Kajal
}
You can use JQuery each function:
$.each(myData.row, function(index,item) {
// here you can extract the data
alert (item.zipcode);
});
Use JSON.parse(jsonstring). Then iterate over the objects/arrays.
Related
This the data i need this value only email": "gm#gmail.com"
{
"params": {
"user": {
"address1": "790 7th Ave",
"address2": "hhhdkhdskhsdkh",
"city": "Chennai",
"name": "gm4",
"phone": "",
"state": "TN",
"zipcode": "600008"
},
"query": {
"FILTERS": [
[
{
"EQ": {
"email": "gm#gmail.com"
}
}
]
],
"PAGENUM": 1,
"PAGESIZE": 100,
"SELECTCOLUMNS": [],
"SORT": []
},
"trigger": "User::UserUpdated"
},
"context": {}
}
actually, I tried const out = req.params.query.FILTERS[0].EQ.email
but i field unable to get expect result plz help.
It is a nested array.
req.params.query.FILTERS[0][0].EQ.email
multiple JSON filter with one function.
I am doing geocoding project now.. i can filter with 1 json file.. but how about have multiple json file with filter ?
state1.json
[{
"no": "1",
"location": "Acheh Baru",
"postcode": "32000",
"postOffice": "Sitiawan",
"state": "Perak"
}, {
"no": "2",
"location": "Akauntan Negeri",
"postcode": "30594",
"postOffice": "Ipoh",
"state": "Perak"
}, {
"no": "3",
"location": "Alor Kechor",
"postcode": "32800",
"postOffice": "Parit",
"state": "Perak"
}]
state2.json
[
{
"no": "1",
"location": "Air Putih",
"postcode": "83400",
"postOffice": "Seri Medan",
"state": "Johor"
},
{
"no": "2",
"location": "Akauntan Negeri",
"postcode": "80594",
"postOffice": "Johor Bahru",
"state": "Johor"
},
{
"no": "3",
"location": "Aked Mara",
"postcode": "83100",
"postOffice": "Rengit",
"state": "Johor"
}
]
state3.json
[
{
"no": "1",
"location": "Akauntan Negeri",
"postcode": "05594",
"postOffice": "Alor Setar",
"state": "Kedah"
},
{
"no": "2",
"location": "Alor Gelegah",
"postcode": "05400",
"postOffice": "Alor Setar",
"state": "Kedah"
},
{
"no": "3",
"location": "Alor Ibus Tepi Laut",
"postcode": "06600",
"postOffice": "Kuala Kedah",
"state": "Kedah"
}
]
I able to filter with 1 json to get value that i want :
import data from '../json/state1.json';
let jsonString = JSON.stringify(data);
let parsedData = JSON.parse(jsonString);
function findObjectByKey(parsedData, key, value) {
for (var i = 0; i < parsedData.length; i++) {
if (parsedData[i][key] === value) {
return parsedData[i]['state'];
}
}
return null;
}
let obj = findObjectByKey(parsedData, 'codes', "34200");
console.log(obj); //output Perak
My question
How to filter with state2.json and state3.json together? if I type 83100, should output Johor. I tried to import all data in one variable but still not able to filter the value.
for some reason, I need to separate multiple JSON.
Import all the JSON data in your file. Using array#concat join all the data in a single array. Using array#find you can get the first matched object for a given key and value.
If you are interested in getting all the matched object, replace the array#find with array#filter.
import data1 from '../json/state1.json';
import data2 from '../json/state2.json';
import data3 from '../json/state3.json';
function findObjectByKey(data, key, value) {
return data.find(state => state[key] === value);
}
let obj = findObjectByKey([].concat(data1,data2,data3), 'postcode', "34200");
My original project had only one level of JSON data but it has grown and I need a second nested layer.
From what I researched it seemed that I could simply take the original object.name and append it with the new member level I needed to create like object.members.name. However this spits back an error undefined.
Clearly something is wrong or missing and I've been scratching my head for a while now.
I checked my JSON formatting a bunch of times and ran it through a validator (which checked out OK), no errors show up in the console and any search I make seems to bring up a much more complicated scenario then mine. I can't seem to narrow down what the problem is.
Any help or direction is much appreciated!
Here are the code blocks:
Non working multi level
Working single level
Non working multi level JSON data version
var obj =
{
"results": [
{
"members": [
{
"name": "John Smith",
"state": "NY",
"phone": "555-555-1111"
},
{
"name": "Mary Jones",
"state": "PA",
"phone": "555-555-2222"
},
{
"name": "Edward Edwards",
"state": "NY",
"phone": "555-555-3333"
},
{
"name": "Abby Abberson",
"state": "RI",
"phone": "555-555-4444"
}
]
}
]
};
$(obj.results).each(function(k,object){
var output = $("div.output");
output.append(object.members.name + " ");
output.append(object.members.phone + "<br />");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="output"></div>
Working single level JSON data version
var obj = {
"results": [
{
"name": "John Smith",
"state": "NY",
"phone": "555-555-1111"
},
{
"name": "Mary Jones",
"state": "PA",
"phone": "555-555-2222"
},
{
"name": "Edward Edwards",
"state": "NY",
"phone": "555-555-3333"
},
{
"name": "Abby Abberson",
"state": "RI",
"phone": "555-555-4444"
}
]
};
$(obj.results).each(function(k,object){
var output = $("div.output");
output.append(object.name + " ");
output.append(object.phone + "<br />");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="output"></div>
Nest your each statements?
var obj = {
"results": [{
"members": [{
"name": "John Smith",
"state": "NY",
"phone": "555-555-1111"
},
{
"name": "Mary Jones",
"state": "PA",
"phone": "555-555-2222"
},
{
"name": "Edward Edwards",
"state": "NY",
"phone": "555-555-3333"
},
{
"name": "Abby Abberson",
"state": "RI",
"phone": "555-555-4444"
}
]
}]
};
$(obj.results).each(function(k, result) {
var output = $("div.output");
$(result.members).each(function(index, member) {
output.append(member.name + " ");
output.append(member.phone + "<br />");
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="output">
</div>
Of course, I'd avoid using names like 'object' or 'obj' -- use result and member instead. Far more mnemonic. You'll know exactly what it refers to.
I have an array in the below format which I need to filter based on the key and value. I am planning to use Underscore.js for this. I tried using the following code but it searches for all the fields value but I need to search based on the key and value and not in all the keys. How to use Underscore for this?
Please let me know.
var sortedArray = _.sortBy(_.filter(arr, function (obj) {
return _.values(obj).some(function (el) {
return (typeof el ==="string" && el.match(new RegExp(searchStr, "i")));
});
}), function (obj){
return obj[colName];
});
}
{
"recordsTotal": 5,
"recordsFiltered": 5,
"aaData": [
{
"firstname": "Pradeep",
"lastname": "Kumar",
"city": "Bangalore",
"country": "India"
},
{
"firstname": "John",
"lastname": "Wells",
"city": "Calcutta",
"country": "India"
},
{
"firstname": "Praveen",
"lastname": "Garg",
"city": "columbo",
"country": "Srilanka"
},
{
"firstname": "Joe",
"lastname": "Wells",
"city": "Luton",
"country": "UK"
},
{
"firstname": "Rita",
"lastname": "Wahlin",
"city": "houston",
"country": "USA"
}
]
}
Sorry if I was not able to post the question properly. I found a solution and the is using the below code.
var searchColData=function(arr, searchStr, colName){
var sortedArray = _.filter(arr, function (obj) {
return (_.property(colName)(obj).toString().match(new RegExp(searchStr, "i")));
});
return sortedArray;
}
Hope this helps someone.
Hi i'm relatively new to JS and keep getting the error missing] after element list when I try and run the following:
Have a JSON file called test.JSON:
{
"156644": {
name: "name1",
"street": "street1",
"city": "city1"
},
"68656": {
"name": "name2 ",
"street": "street2",
"city": "city1"
},
"388655": {
"name": "name3",
"street": "street3",
"city": "city1"
},
"4564": {
"name": "name4",
"street": "street4",
"city": "city1"
},
"6333": {
"name": "name5",
"street": "street5",
"city": "city1"
}
}
And some Javascript:
var myObject = eval("(" + $.getJSON("test.json") + ")");
How can I fix this error?
The getJSON function:
Will parse JSON into a JavaScript object for you. Don't touch eval.
Is asynchronous and handles the data via a callback not a return value
So:
$.getJSON("test.json", handleData);
function handleData(data) {
console.log(data);
}
You also have a syntax error in your JSON data. Use http://jsonlint.com/ to pinpoint it.