10-06-22 03:55 PM
Hi, I'd like to extract a collection of zip codes from a JSON response such as:
https://jsonplaceholder.typicode.com/users
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
}
]
I can get single value but a collection fails at run time, though compiler didn't find syntax error.
I tried $[?(@.address.zipcode)] which also results in error.
Could someone please advise the correct query?
10-06-22 04:44 PM
10-06-22 05:16 PM
10-06-22 05:50 PM
14-06-22 05:17 PM
JArray jsonText = JArray.Parse(Response_Content);
DataTable dtZipCodes = new DataTable();
DataColumn column = new DataColumn("zipcode");
column.DataType = System.Type.GetType("System.String");
dtZipCodes.Columns.Add(column);
IEnumerable<JToken> zips = jsonText.SelectTokens("
.address.zipcode");
foreach (JToken zipcode in zips)
{
DataRow row = dtZipCodes.NewRow();
row["zipcode"] = (string)zipcode;
dtZipCodes.Rows.Add(row);
}
ZipCodes = dtZipCodes;