2
$\begingroup$

Here's my problem: I've got a JSON-formatted response and want to match some variables over the response. I'm doing it using Cases:

JsonResponse := Import["https://reverse.geocoder.api.here.com/6.2/reversegeocode.json?&app_code=djPZyynKsbTjIUDOBcHZ2g&app_id=xWVIueSv6JL0aJ5xqTxb&gen=3&int=true&jsonAttributes=1&languages=it-it&maxresults=9&mode=retrieveAddresses&prox=43.605979206920594,13.513478915691394,100", "JSON"]

Points = Cases[JsonResponse, {___ "displayPosition" -> "{latitude" -> lat_,"longitude" -> long_ "}", "navigationPosition" -> "{{latitude" -> _Float, "longitude" -> _Float "}}", "mapView" -> "{bottomRight" -> "{latitude" -> _Float, "longitude" -> _Float "}", "topLeft" -> "{latitude" -> _Float, "longitude" -> _Float "}}", "locationId" -> _String, "locationType" -> "point", "address" -> "{city" -> _String, "label" -> _String, "country" -> _String, "county" -> county_, "state" -> state_, "street" -> st_, "houseNumber" -> num_, postalCode -> zip_ ___} -> {{lat, long} -> {st, num, zip, county, state}}, Infinity]

I need to make it consider the inner braces as part of the pattern to be matched (Mathematica parses them as meaning list, otherwise).

$\endgroup$
4
  • $\begingroup$ It looks like maybe what you want is Cases[JsonResponse, "distance" -> dist_ :> dist, Infinity] $\endgroup$
    – mfvonh
    Commented Sep 22, 2015 at 23:29
  • $\begingroup$ @mfvonh the code strangely returns a void list, i'm gonna show the rest of the code $\endgroup$
    – JavierG
    Commented Sep 22, 2015 at 23:32
  • $\begingroup$ I don't see your point trying to do that. The natural way is matching the lists $\endgroup$ Commented Sep 23, 2015 at 3:07
  • $\begingroup$ @belisarius could you give me an example? $\endgroup$
    – JavierG
    Commented Sep 23, 2015 at 10:12

1 Answer 1

3
$\begingroup$

I might use Fold to go down the nested rules. At some levels of the data, a list of rules creates an extra set of braces. The Flatten /@ First@... seemed a convenient fix.

locations = Flatten /@ First@
    Fold[
     #2 /. #1 &,
     JsonResponse,
     {"response", "view", "result", 
      "location", {"displayPosition", "address"}}];

{{"latitude", "longitude"} -> {"label", "houseNumber", "postalCode", 
    "county", "state"}} /. locations
(*
{{{43.6062, 13.5135} -> {"Via Luigi Paolucci, 8, 60125 Ancona AN, Italia", "8", "60125", "AN", "Marche"}},
 {{43.6062, 13.5131} -> {"Via Valle Miano, 32, 60125 Ancona AN, Italia", "32", "60125", "AN", "Marche"}},
 {{43.6061, 13.5141} -> {"Svincolo Miano, 60125 Ancona AN, Italia", "houseNumber", "60125", "AN", "Marche"}},
 {{43.6065, 13.5132} -> {"Largo Giovanni Bovio, 2, 60125 Ancona AN, Italia", "2", "60125", "AN", "Marche"}},
 {{43.6062, 13.5128} -> {"Piazzale Bovio Giovanni, 3, 60125 Ancona AN, Italia", "3", "60125", "AN", "Marche"}},
 {{43.606, 13.5144} -> {"Asse Nord Sud, 60125 Ancona AN, Italia", "houseNumber", "60125", "AN", "Marche"}},
 {{43.6059, 13.5144} -> {"Asse Nord Sud, 60125 Ancona AN, Italia", "houseNumber", "60125", "AN", "Marche"}},
 {{43.606, 13.5144} -> {"Asse Nord Sud, 60125 Ancona AN, Italia", "houseNumber", "60125", "AN", "Marche"}},
 {{43.6062, 13.5144} -> {"Asse Nord Sud, 60125 Ancona AN, Italia", "houseNumber", "60125", "AN", "Marche"}}}
*)

Edit: Addendum

Note: In some cases "houseNumber" is missing. As an alternative to DeleteCases mentioned by LLlAMnYP, one could relace unreplaced keys by Sequence.

address = {"label", "houseNumber", "postalCode", "county", "state"};
{{"latitude", "longitude"} -> address} /. locations /. 
 Alternatives @@ address :> Sequence[]
$\endgroup$
4
  • $\begingroup$ Great, thanks! What if I had to remove the entries without the house number? $\endgroup$
    – JavierG
    Commented Sep 23, 2015 at 10:59
  • 2
    $\begingroup$ Something like DeleteCases[{_ -> {_, "houseNumber", __}}] $\endgroup$
    – LLlAMnYP
    Commented Sep 23, 2015 at 11:42
  • 1
    $\begingroup$ Forgot to ping @Piruzzolo :) $\endgroup$
    – LLlAMnYP
    Commented Sep 23, 2015 at 11:43
  • $\begingroup$ @LLlAMnYP Thank you! $\endgroup$
    – JavierG
    Commented Sep 23, 2015 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.