parsing a specific JSON data - php

i am making a android application n use a .php file to call java script file that returns me a JSON output, now my problem is the output is in a valid JSON format, but i am confused as to how to parse the values. the output is
{
"destination_addresses" : [
"Kambarganvi road, Durgadakeri, Karnataka 580011, India",
"St Inez Road, Santa Inez, Panjim, Goa 403001, India",
"Dr Braganza Pereira Road, Santa Inez, Panjim, Goa 403001, India",
"Dayanand Bandodkar Marg, Santa Inez, Panjim, Goa 403001, India",
"St Inez Road, Santa Inez, Panjim, Goa 403001, India",
"18th June Road, Santa Inez, Panjim, Goa 403001, India"
],
"origin_addresses" : [ "Suyog Residency, St Joaquim Road, Borda, Margao, Goa 403602, India" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "133 km",
"value" : 132717
},
"duration" : {
"text" : "1 day 4 hours",
"value" : 100199
},
"status" : "OK"
},
{
"distance" : {
"text" : "33.4 km",
"value" : 33427
},
"duration" : {
"text" : "6 hours 54 mins",
"value" : 24832
},
"status" : "OK"
},
{
"distance" : {
"text" : "33.5 km",
"value" : 33518
},
"duration" : {
"text" : "6 hours 55 mins",
"value" : 24923
},
"status" : "OK"
},
{
"distance" : {
"text" : "33.8 km",
"value" : 33834
},
"duration" : {
"text" : "7 hours 0 mins",
"value" : 25170
},
"status" : "OK"
},
{
"distance" : {
"text" : "33.5 km",
"value" : 33503
},
"duration" : {
"text" : "6 hours 55 mins",
"value" : 24893
},
"status" : "OK"
},
{
"distance" : {
"text" : "33.5 km",
"value" : 33457
},
"duration" : {
"text" : "6 hours 55 mins",
"value" : 24880
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
i want to parse the values in such a way that i can place the distances of the result should be put in a data structure like an array and then sort them.
Thank you in advance.

use this to make own model .
http://www.jsonschema2pojo.org/
after making model you can use bwlow code.
Gson gson = new Gson();
gson.fromJson(jData, AddPostModel.class);
AddPostModel have all the data which comes from server.

Related

How to parse Google Directions API Json in PHP, specifically the Distance-Values and Duration-Values for each "Step" and "Leg" which are all nested?

The json response can have any number of Distance-Values, Duration-Values, Steps and Legs nested within each other. I need to ensure the original pairing of Distance and Duration in a variable for later math. Note the file also has Distance-Values and Duration-Values NOT nested within the Steps that I am not interested in collecting. I am a newbie PHP programmer - please be gentle. Thank you.
Here is a sample json file response, apologies it is so long.
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJq0F7QdOGwokRtGWVrOp28h0",
"types" : [ "premise" ]
},
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJvzh05jM_6IkRhcy9aqUcTHM",
"types" : [ "establishment", "point_of_interest", "university" ]
},
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJ28a23cMv6IkRLzOqZ0h6vwo",
"types" : [ "premise" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 40.9003613,
"lng" : -73.11792029999999
},
"southwest" : {
"lat" : 40.7791055,
"lng" : -73.5611243
}
},
"copyrights" : "Map data ©2020 Google",
"legs" : [
{
"distance" : {
"text" : "28.7 mi",
"value" : 46179
},
"duration" : {
"text" : "40 mins",
"value" : 2424
},
"end_address" : "100 Nicolls Rd, Stony Brook, NY 11794, USA",
"end_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"start_address" : "99 Jericho Turnpike, Westbury, NY 11590, USA",
"start_location" : {
"lat" : 40.7791055,
"lng" : -73.56088389999999
},
"steps" : [
{
"distance" : {
"text" : "128 ft",
"value" : 39
},
"duration" : {
"text" : "1 min",
"value" : 14
},
"end_location" : {
"lat" : 40.7794066,
"lng" : -73.5611243
},
"html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e toward \u003cb\u003eJericho Turnpike\u003c/b\u003e",
"polyline" : {
"points" : "mt{wFnj~_M{#n#"
},
"start_location" : {
"lat" : 40.7791055,
"lng" : -73.56088389999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.4 mi",
"value" : 629
},
"duration" : {
"text" : "3 mins",
"value" : 158
},
"end_location" : {
"lat" : 40.7821022,
"lng" : -73.5545772
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eJericho Turnpike\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "iv{wF~k~_Me#oAg#wAQe#M_#]_Ag#sAQe#g#wA[s#K[a#eAWu#Qs#I[m#gCi#{BAEk#{Bg#yB"
},
"start_location" : {
"lat" : 40.7794066,
"lng" : -73.5611243
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 251
},
"duration" : {
"text" : "1 min",
"value" : 15
},
"end_location" : {
"lat" : 40.7827402,
"lng" : -73.55172809999999
},
"html_instructions" : "Slight \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eS Marginal Rd\u003c/b\u003e",
"maneuver" : "turn-slight-right",
"polyline" : {
"points" : "cg|wFbc}_MEe#AGAECKIc#I_#IYEY[mBKy#Ik#Ge#E_#EYAYCYAY"
},
"start_location" : {
"lat" : 40.7821022,
"lng" : -73.5545772
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 mi",
"value" : 218
},
"duration" : {
"text" : "1 min",
"value" : 13
},
"end_location" : {
"lat" : 40.7831579,
"lng" : -73.54922080000001
},
"html_instructions" : "Take the \u003cb\u003eInterstate 495 E\u003c/b\u003e ramp on the \u003cb\u003eleft\u003c/b\u003e to \u003cb\u003eRiverhead\u003c/b\u003e",
"maneuver" : "ramp-left",
"polyline" : {
"points" : "ck|wFhq|_MIYAEAICMASAMKeAKuAC]Cm#AAAYCi#Cs#AK?GAIAGACCEEI"
},
"start_location" : {
"lat" : 40.7827402,
"lng" : -73.55172809999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.3 mi",
"value" : 2154
},
"duration" : {
"text" : "1 min",
"value" : 83
},
"end_location" : {
"lat" : 40.7877687,
"lng" : -73.5245628
},
"html_instructions" : "Merge onto \u003cb\u003eI-495 E\u003c/b\u003e",
"maneuver" : "merge",
"polyline" : {
"points" : "wm|wFra|_MW{MWeIEq#]eIWeFOcCG{#UmD_#cFU}BQaBQaBQeBuAsMc#iDGe#Gi#c#}Cg#mDg#cD[mBc#kCYgB]mBa#cC_#kBi#eC[kA[mAa#yAOg#Wy#Qi#]eA"
},
"start_location" : {
"lat" : 40.7831579,
"lng" : -73.54922080000001
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.4 mi",
"value" : 679
},
"duration" : {
"text" : "1 min",
"value" : 34
},
"end_location" : {
"lat" : 40.79059609999999,
"lng" : -73.5181288
},
"html_instructions" : "Take exit \u003cb\u003e42\u003c/b\u003e for \u003cb\u003eBroadway\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eNorthern Pkwy E\u003c/b\u003e toward \u003cb\u003eHauppauge\u003c/b\u003e",
"maneuver" : "ramp-right",
"polyline" : {
"points" : "qj}wFngw_MAa#AGK[OIOGMEMOC"
},
"start_location" : {
"lat" : 40.7877687,
"lng" : -73.5245628
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "16.0 mi",
"value" : 25810
},
"duration" : {
"text" : "16 mins",
"value" : 945
},
"end_location" : {
"lat" : 40.829462,
"lng" : -73.25121489999999
},
"html_instructions" : "Merge onto \u003cb\u003eNorthern State Pkwy\u003c/b\u003e",
"maneuver" : "merge",
"polyline" : {
"points" : "g|}wFh_v_Me#wAK[a#]#[#[#]#YBWBU#W#CB]DYD_#NaB"
},
"start_location" : {
"lat" : 40.79059609999999,
"lng" : -73.5181288
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.3 mi",
"value" : 463
},
"duration" : {
"text" : "1 min",
"value" : 18
},
"end_location" : {
"lat" : 40.8278228,
"lng" : -73.2461648
},
"html_instructions" : "Merge onto \u003cb\u003eNY-347\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eNY-454 E\u003c/b\u003e",
"maneuver" : "merge",
"polyline" : {
"points" : "coexF`{a~LBQBOF_#H_#F[Ha#H_#Ji#Je#H_#XmA\\oAVkA~CwM"
},
"start_location" : {
"lat" : 40.829462,
"lng" : -73.25121489999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.8 mi",
"value" : 2820
},
"duration" : {
"text" : "3 mins",
"value" : 163
},
"end_location" : {
"lat" : 40.8243168,
"lng" : -73.21340239999999
},
"html_instructions" : "Continue straight to stay on \u003cb\u003eNY-347\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eNY-454 E\u003c/b\u003e",
"maneuver" : "straight",
"polyline" : {
"points" : "{dexFn{`~L^_Bf#aCLi#b#{BlAyF^kB`#kBFWTeAd#gC`#gCN{#l#cF?IJcAPcCNoB`#eFTwCFcAXkD#[Dq#|#aMB[XiDVkDDw#NiCJyCHgD#gA#qA?c#?uAA[AqA?WC}#K{DEqAGmCAc#CqAGkBMcFA_#E_ACaACaAIoBA]A}A?qB?Y#oDH_EDkBVsDJsA"
},
"start_location" : {
"lat" : 40.8278228,
"lng" : -73.2461648
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "6.2 mi",
"value" : 9936
},
"duration" : {
"text" : "11 mins",
"value" : 689
},
"end_location" : {
"lat" : 40.8747687,
"lng" : -73.12067499999999
},
"html_instructions" : "Keep \u003cb\u003eleft\u003c/b\u003e to continue on \u003cb\u003eNY-347 E\u003c/b\u003e, follow signs for \u003cb\u003ePort Jefferson\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Carvel (on the right)\u003c/div\u003e",
"maneuver" : "keep-left",
"polyline" : {
"points" : "_odxFvnz}LFaD#o#Iw[q#yAeD_AqBk#oA{#mBoBmEc#cAoDwHQ_#"
},
"start_location" : {
"lat" : 40.8243168,
"lng" : -73.21340239999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 281
},
"duration" : {
"text" : "1 min",
"value" : 34
},
"end_location" : {
"lat" : 40.8763118,
"lng" : -73.1180346
},
"html_instructions" : "Continue onto \u003cb\u003eSmithtown Bypass\u003c/b\u003e",
"polyline" : {
"points" : "ijnxFdkh}LcAuBQ]eCyEo#wAg#iA"
},
"start_location" : {
"lat" : 40.8747687,
"lng" : -73.12067499999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.8 mi",
"value" : 2899
},
"duration" : {
"text" : "4 mins",
"value" : 258
},
"end_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eStony Brook Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "}snxFtzg}LIU]TmBx#WJo#XiAd#IDqB|#sAj#sF`CoFzBaHvCmF|BaDrAw#`#wDbBiAt#gBpAMNo#d#_#TiBpAcAt#cCbB]VUN}BxAUNe#VoAp#aCnAcDdBiAl#}Ax#MHi#Tc#PkA\\wCv#yA`#{Ab#qAZqAVkCf#_BZWFiB\\uAXiB`#e#JeB\\[HMBSFA?WJ_#Tc#ZML"
},
"start_location" : {
"lat" : 40.8763118,
"lng" : -73.1180346
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
},
{
"distance" : {
"text" : "13.7 mi",
"value" : 21984
},
"duration" : {
"text" : "25 mins",
"value" : 1473
},
"end_address" : "18 Hauppauge Rd, Commack, NY 11725, USA",
"end_location" : {
"lat" : 40.8286064,
"lng" : -73.2937987
},
"start_address" : "100 Nicolls Rd, Stony Brook, NY 11794, USA",
"start_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"steps" : [
{
"distance" : {
"text" : "1.8 mi",
"value" : 2870
},
"duration" : {
"text" : "4 mins",
"value" : 258
},
"end_location" : {
"lat" : 40.87645940000001,
"lng" : -73.1181333
},
"html_instructions" : "Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eStony Brook Rd\u003c/b\u003e toward \u003cb\u003eDevelopment Drive\u003c/b\u003e",
"polyline" : {
"points" : "gjsxFfij}LLMb#[^UVK#?RGLCZIdB]d#KhBa#tAYhB]VG~A[jCg#pAWpA[zAc#xAa#vCw#jA]b#Qh#ULI|Ay#hAm#bDeB`CoAnAq#d#WTO|ByATO\\WbCcBbAu#hBqA^Un#e#VK`BiAjAq#tDaBx#_#nAg#pAi#lF}BpEmBpAk#nF{BrF_C`Bs#jBy#pAi#`Ac#nB{#"
},
"start_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "3.6 mi",
"value" : 5810
},
"duration" : {
"text" : "8 mins",
"value" : 456
},
"end_location" : {
"lat" : 40.8469852,
"lng" : -73.17149669999999
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eNY-347 W\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eSmithtown Bypass\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "{tnxFh{g}LhD~Gp#tCh#xA~#|B"
},
"start_location" : {
"lat" : 40.87645940000001,
"lng" : -73.1181333
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "4.6 mi",
"value" : 7390
},
"duration" : {
"text" : "7 mins",
"value" : 430
},
"end_location" : {
"lat" : 40.8288007,
"lng" : -73.2481703
},
"html_instructions" : "Continue straight to stay on \u003cb\u003eNY-347 W\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eSmithtown Bypass\u003c/b\u003e",
"maneuver" : "straight",
"polyline" : {
"points" : "u|hxFzhr}Lj#fA|EVQv#sAjGs#|CS~#WbA"
},
"start_location" : {
"lat" : 40.8469852,
"lng" : -73.17149669999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "2.9 mi",
"value" : 4593
},
"duration" : {
"text" : "3 mins",
"value" : 177
},
"end_location" : {
"lat" : 40.8186598,
"lng" : -73.2952425
},
"html_instructions" : "Keep \u003cb\u003eleft\u003c/b\u003e to continue on \u003cb\u003eNorthern State Pkwy\u003c/b\u003e",
"maneuver" : "keep-left",
"polyline" : {
"points" : "_kexF`ha~L[pE`BI`D"
},
"start_location" : {
"lat" : 40.8288007,
"lng" : -73.2481703
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 mi",
"value" : 176
},
"duration" : {
"text" : "1 min",
"value" : 21
},
"end_location" : {
"lat" : 40.8195393,
"lng" : -73.29465789999999
},
"html_instructions" : "Take exit \u003cb\u003e43\u003c/b\u003e for \u003cb\u003eSuffolk County 4\u003c/b\u003e toward \u003cb\u003eCommack\u003c/b\u003e",
"maneuver" : "ramp-right",
"polyline" : {
"points" : "skcxFfnj~LMb#ADADADABABCBABCBGDABC#GBC#E?C#A?A?C?C?C?AAE?CACAECECGECCAECCACCICICOCUOkAIe#"
},
"start_location" : {
"lat" : 40.8186598,
"lng" : -73.2952425
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "299 ft",
"value" : 91
},
"duration" : {
"text" : "1 min",
"value" : 19
},
"end_location" : {
"lat" : 40.8197276,
"lng" : -73.29361089999999
},
"html_instructions" : "Keep \u003cb\u003eleft\u003c/b\u003e at the fork, follow signs for \u003cb\u003eCounty Road 4 N\u003c/b\u003e",
"maneuver" : "fork-left",
"polyline" : {
"points" : "cqcxFrjj~LQ{AOyAC["
},
"start_location" : {
"lat" : 40.8195393,
"lng" : -73.29465789999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.6 mi",
"value" : 997
},
"duration" : {
"text" : "2 mins",
"value" : 92
},
"end_location" : {
"lat" : 40.82857569999999,
"lng" : -73.29312469999999
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eCommack Rd\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "ircxF`dj~L?IAQKA{#CE?cAA_AC_AC_#CqEMw#Cw#A_BAYFWAQ?cCGsDK{FOsCIs#A[#Q?gA#E#k#BU#U#"
},
"start_location" : {
"lat" : 40.8197276,
"lng" : -73.29361089999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "187 ft",
"value" : 57
},
"duration" : {
"text" : "1 min",
"value" : 20
},
"end_location" : {
"lat" : 40.8286064,
"lng" : -73.2937987
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eHauppauge Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "siexF~`j~LAp#An#Ad#"
},
"start_location" : {
"lat" : 40.82857569999999,
"lng" : -73.29312469999999
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "mt{wFnj~_M{#n#e#oAy#tA"
},
"summary" : "Northern State Pkwy and NY-347",
"warnings" : [],
"waypoint_order" : [ 0 ]
}
],
"status" : "OK"
}
I figured this out by using multiple nested "foreach" loops with several "if" tests. I'm sure it's not the most elegant but it works. The key (pun intended) was keeping track of all the index/keys for each nested foreach loop.

Group by age groups in mongo db

I am using following mongo db query . and it is showing me only age groups of my personas data.
db.amplifyindex.aggregate([
{ $unwind: "$demographic" },
{ $match : { 'demographic.is_latest':"active",
'demographic.date_of_birth' : { $exists : true} } },
{ $project : {"ageInMillis" : {$subtract : [new Date(),
"$demographic.date_of_birth"] } } },
{ $project : {"age" : {$divide : ["$ageInMillis", 31558464000] }}},
{ $project : {"age" : {$subtract : ["$age", {$mod : ["$age",1]}]}}},
])
The result object by this query is:
{
"_id" : ObjectId("58a42cbbdb5d880c1e000029"),
"age" : 29.0
}
I want to get data in a way as :
{
"_id" : ObjectId("58a42cbbdb5d880c1e000029"),
"age" : 10-30
"personas" : 10
}
Complete data structure in my scenario is given as :
{
"_id" : ObjectId("58a42cbbdb5d880c1e000029"),
"persona_email" : "milton.ullrich#gmail.com",
"company_id" : "1",
"date_added" : ISODate("2017-02-15T10:23:15.000Z"),
"demographic" : [
{
"persona_fname" : "Hayden",
"middle_name" : "Jacobs",
"persona_lname" : "Schmeler",
"gender" : "male",
"date_of_birth" : ISODate("1987-06-16T19:00:00.000Z"),
"marital_status" : "single",
"height" : "2.1 feet",
"weight" : "5 kg",
"measurement" : {
"waist" : "34 inch"
},
"disabilities" : "No",
"race" : "Asian",
"nationality" : "Jordan",
"life_cycle" : "empty nest",
"children" : {
"gender" : "female",
"date_of_birth" : ISODate("1987-06-16T19:00:00.000Z")
},
"medicare_no" : "4916725587565",
"driving_licence_no" : "PACBALM420M",
"id_no" : "4485872783336",
"passport_no" : "OCRFHCNR",
"residential_address" : {
"unit_no" : 603,
"street_no" : "34863 Ondricka Viaduct Apt. 154",
"street_name" : "84799 Little Wall",
"suburb" : "West Virginia",
"postal_code" : "23873",
"state" : "Dominica",
"country" : "Cook Islands"
},
"work_address" : {
"unit_no" : "6011865161287875",
"street_no" : "630 Beer Underpass Suite 372",
"street_name" : "87672 Lind Burg",
"suburb" : "West Virginia",
"postal_code" : "84356-3662",
"state" : "Qatar",
"country" : "British Indian Ocean Territory (Chagos Archipelago)"
},
"shipping_address" : {
"unit_no" : "6011865161287875",
"street_no" : "8292 Langosh Drive Suite 065",
"street_name" : "9844 Nicolas Mount",
"suburb" : "West Virginia",
"postal_code" : "07014",
"state" : "Togo",
"country" : "Tunisia"
},
"job_title" : "Geoscientists",
"employer_name" : "Aufderhar Group",
"income" : 72577,
"phone_numbers" : {
"work_phone" : "993-783-7499",
"home_phone" : "701.546.7016",
"mobile" : "1-346-729-4392"
},
"emails" : {
"work_emails" : "nikolaus.asa#gmail.com",
"personal_emails" : "emmitt68#gmail.com"
},
"languages" : "italian",
"data_source" : "soldi",
"date_added" : ISODate("2017-02-15T10:23:15.000Z"),
"source" : "soldi",
"is_latest" : "inactive"
},
{
"persona_fname" : "Julien",
"middle_name" : "Hirthe",
"persona_lname" : "Schaefer",
"gender" : "female",
"date_of_birth" : ISODate("1987-06-16T19:00:00.000Z"),
"marital_status" : "single",
"height" : "2.1 feet",
"weight" : "5 kg",
"measurement" : {
"waist" : "34 inch"
},
"disabilities" : "No",
"race" : "Asian",
"nationality" : "Sudan",
"life_cycle" : "single",
"children" : {
"gender" : "female",
"date_of_birth" : ISODate("1987-06-16T19:00:00.000Z")
},
"medicare_no" : "4024007131689860",
"driving_licence_no" : "OGOSUOIFSDN",
"id_no" : "5523297913341227",
"passport_no" : "WWZYTE489ZR",
"residential_address" : {
"unit_no" : 603,
"street_no" : "338 Mueller Gardens Suite 397",
"street_name" : "6302 Catalina Isle",
"suburb" : "West Virginia",
"postal_code" : "97534",
"state" : "Antarctica (the territory South of 60 deg S)",
"country" : "Turks and Caicos Islands"
},
"work_address" : {
"unit_no" : "6011865161287875",
"street_no" : "8561 Jesus Ridges Apt. 662",
"street_name" : "1869 Josiah Wall Apt. 347",
"suburb" : "West Virginia",
"postal_code" : "14810",
"state" : "Zambia",
"country" : "Mongolia"
},
"shipping_address" : {
"unit_no" : "6011865161287875",
"street_no" : "514 Heller Center",
"street_name" : "835 Paxton Cliffs Suite 040",
"suburb" : "West Virginia",
"postal_code" : "48942-3845",
"state" : "Palau",
"country" : "Belize"
},
"job_title" : "Geoscientists",
"employer_name" : "Aufderhar Group",
"income" : 72577,
"phone_numbers" : {
"work_phone" : "(553) 892-7614 x573",
"home_phone" : "796.308.3001 x88799",
"mobile" : "+1-876-339-1755"
},
"emails" : {
"work_emails" : "prohaska.katelynn#hotmail.com",
"personal_emails" : "ozulauf#jenkins.com"
},
"languages" : "german",
"data_source" : "soldi",
"date_added" : ISODate("2017-02-15T10:23:15.000Z"),
"source" : "soldi",
"is_latest" : "active"
}
]
}
Within your group pipeline, create the age ranges as part of the _id key and this can be done through the use of the $concat and "$cond" operators. Consider running the following pipeline to get the desired result:
db.amplifyindex.aggregate([
{ "$unwind": "$demographic" },
{ "$match": { "demographic.is_latest": "active" } },
{
"$project": {
"age": {
"$divide": [
{
"$subtract": [
new Date(),
{ "$ifNull": ["$demographic.date_of_birth", new Date()] }
]
},
1000 * 86400 * 365
]
}
}
},
{
"$group": {
"_id": {
"$concat": [
{ "$cond": [ { "$lte": [ "$age", 0 ] }, "Unknown", ""] },
{ "$cond": [ { "$and": [ { "$gt": ["$age", 0 ] }, { "$lt": ["$age", 10] } ]}, "Under 10", ""] },
{ "$cond": [ { "$and": [ { "$gte": ["$age", 10] }, { "$lt": ["$age", 31] } ]}, "10 - 30", ""] },
{ "$cond": [ { "$and": [ { "$gte": ["$age", 31] }, { "$lt": ["$age", 51] } ]}, "31 - 50", ""] },
{ "$cond": [ { "$and": [ { "$gte": ["$age", 51] }, { "$lt": ["$age", 71] } ]}, "51 - 70", ""] },
{ "$cond": [ { "$gte": [ "$age", 71 ] }, "Over 70", ""] }
]
},
"personas": { "$sum": 1 }
}
},
{ "$project": { "_id": 0, "age": "$_id", "personas": 1 } }
])
In the above pipeline, I've taken out the query for null date fields and replaced it with the $ifNull operator within the $project pipeline. This will return the current date where the birthday field is null and thus yields an age of 0 years which will fall into a special age range bracket, thus the need to include all the documents, including the ones where the date of birth field missing.
In the case where the $concat operator is used for all $cond expressions, the combination is crucial as it acts as a case statement where otherwise there will be nested $cond operators.
To understand this design, take the third $cond expression
{
"$cond": [
{
"$and": [
{ "$gte": ["$age", 10] },
{ "$lt": ["$age", 31] }
]
},
"10 - 30", // matching if
"" // else
]
},
which essentially expresses the logic
if ( "$age" >= 10 && "$age" < 31 ) { return "10 - 30"; }
else return "";
$concat is useful when used with the $cond operators as the expression will return an empty value except the matching one, which in turn will return the age group.
For example, an age of 30.645411 would return
"$concat": [ "", "", "10 - 30", "", "", "" ]
which then gives you an _id with value of "10 - 30".

Parse JSONArray in Jquery

I am Creating a webapp where i could get the distance and time of travel using google map API..
https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999
I am using HTTP method as given by google..
Its returning JSON Format as give below
{
"destination_addresses" : [ "Kanyakumari - Panvel Highway, Palathara, Kerala 676501, India" ],
"origin_addresses" : [ "Kanyakumari - Panvel Highway, Randathani, Kerala 676510, India" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "2.5 km",
"value" : 2526
},
"duration" : {
"text" : "4 mins",
"value" : 228
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
How could i extract the distance text and duration text using jquery and alert it..
Use file_get_contents() and json_decode() the output..
$contents = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999');
$data = json_decode($contents,true);
echo 'Distance : '.$data['rows'][0]['elements'][0]['distance']['text'];
echo "<br>";
echo 'Duration : '.$data['rows'][0]['elements'][0]['duration']['text'];
This will give you :
Distance : 2.5 km
Duration : 4 mins

How to print the Google Distance Matrix Results Json Data

How to print the Results from Google Distance matrix iam Getting a results like this:
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "4,8 km",
"value" : 4820
},
"duration" : {
"text" : "17 minutes",
"value" : 1038
},
"status" : "OK"
},
{
"distance" : {
"text" : "11,1 km",
"value" : 11064
},
"duration" : {
"text" : "25 minutes",
"value" : 1506
},
"status" : "OK"
}
]
}
],
And My Php Code to print The Output is
$json = file_get_contents($url);
$result = json_decode($json, true);
for($i=0; $i<count($result['rows']); $i++) {
echo "Kilometers " . $result['rows'][$i]["elements"][$i]['distance']['text'];
}
.Please Help
Given that you haven't included all the response, the method invlolved in the retrieval of the data nor the final desired output it is tricky to answer but I hope the following will give guidance on how to render the output.
This uses example data from the interwebs rather than the partial example code given in the question - a bit rough perhaps but...
$res='{
"destination_addresses" : [
"Commons Way, Bridgewater, NJ 08807, USA",
"Morris Turnpike, Short Hills, NJ 07078, USA",
"Monmouth Mall, Eatontown, NJ 07724, USA",
"Garden State Plaza Blvd, Paramus, NJ 07652, USA",
"Newport Centre Mall, Jersey City, NJ 07302, USA"
],
"origin_addresses" : [ "75 Ninth Ave, New York, NY 10011, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "68.8 km",
"value" : 68781
},
"duration" : {
"text" : "56 mins",
"value" : 3334
},
"duration_in_traffic" : {
"text" : "1 hour 1 min",
"value" : 3687
},
"status" : "OK"
},
{
"distance" : {
"text" : "34.8 km",
"value" : 34806
},
"duration" : {
"text" : "36 mins",
"value" : 2138
},
"duration_in_traffic" : {
"text" : "41 mins",
"value" : 2487
},
"status" : "OK"
},
{
"distance" : {
"text" : "86.3 km",
"value" : 86322
},
"duration" : {
"text" : "1 hour 6 mins",
"value" : 3930
},
"duration_in_traffic" : {
"text" : "1 hour 7 mins",
"value" : 4044
},
"status" : "OK"
},
{
"distance" : {
"text" : "32.3 km",
"value" : 32278
},
"duration" : {
"text" : "33 mins",
"value" : 2009
},
"duration_in_traffic" : {
"text" : "35 mins",
"value" : 2082
},
"status" : "OK"
},
{
"distance" : {
"text" : "6.9 km",
"value" : 6879
},
"duration" : {
"text" : "19 mins",
"value" : 1113
},
"duration_in_traffic" : {
"text" : "24 mins",
"value" : 1444
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}';
/* return as an object for easier notation */
$json=json_decode( $res, false );
$origins = $json->origin_addresses;
$destinations=$json->destination_addresses;
/* if there are multiple rows, use a loop and `$rows[$i]` etc */
$elements=$json->rows[0]->elements;
foreach( $elements as $i => $obj ){
echo '
<div>
<h5>From: '.$origins[ 0 ].' to '.$destinations[ $i ].'</h5>
Distance: '.$obj->distance->text.',
Duration:'.$obj->duration->text.',
In traffic: '.$obj->duration_in_traffic->text.',
Status: '.$obj->status.'
</div>';
}
Here is an example using a nested foreach, this is not the only way to render the output but it works.
$json = file_get_contents($url);
$result = json_decode($json, true);
foreach ($result['rows'][0] as $elements => $element) {
foreach ($element as $key => $value) {
echo $value['distance']['text'];
echo $value['duration']['text'];
echo $value['status'];
echo '<br>';
}
}
All we are doing is accessing a multi-dimensional array using $key => $value and then $value[$key] notation... I would suggest a crash course on how to handle arrays in php, this one is great: https://www.youtube.com/watch?v=57y5maglayQ
You can also look at the overall structure of your array by writing echo '<pre>', var_dump($result), '</pre>;' which will show your array => key pairs.

How to extract a value from this JSON response

I am using the file_get_contents() function in PHP to retrieve the contents on a page containing a JSON response. However, I am struggling to get the "text" data.
$response = file_get_contents(someData)
I can use var_dump(json_decode($response)); to show the data, however, I am trying to get the data from the "text" field only within duration.
So far I have tried
$response[0];
$response->rows[0]->elements[0]->duration[0]->text;
But I cannot seem to get the data. I have pasted the response below
{
"destination_addresses" : [ "Manchester, UK", "Liverpool, Merseyside, UK" ],
"origin_addresses" : [ "London, UK" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "335 km",
"value" : 335444
},
"duration" : {
"text" : "3 hours 36 mins",
"value" : 12955
},
"status" : "OK"
},
{
"distance" : {
"text" : "354 km",
"value" : 354415
},
"duration" : {
"text" : "3 hours 43 mins",
"value" : 13387
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
$response = json_decode(file_get_contents(someData));
$text = $response->rows[0]->elements[0]->duration->text;
var_dump($text);
Your only mistake was a [0] after duration.

Categories