0
votes

Hi I´m in need of help to extract a field from a JSON Helper return

Using the following syntax

tell application "JSON Helper"
    set Resultado to (fetch JSON from Request)  
end tell

I get the following result from a Google Geocoding API request.

{results:{{formatted_address:"Rua Dr. Carmelo D'Agostino, 628 - Jardim Rincão, São Paulo - SP, 02991-040, Brazil", partial_match:true, address_components:{{short_name:"628", long_name:"628", types:{"street_number"}}, {short_name:"Rua Dr. Carmelo D'Agostino", long_name:"Rua Doutor Carmelo D'Agostino", types:{"route"}}, {short_name:"Jardim Rincão", long_name:"Jardim Rincão", types:{"political", "sublocality", "sublocality_level_1"}}, {short_name:"São Paulo", long_name:"São Paulo", types:{"administrative_area_level_2", "political"}}, {short_name:"SP", long_name:"São Paulo", types:{"administrative_area_level_1", "political"}}, {short_name:"BR", long_name:"Brazil", types:{"country", "political"}}, {short_name:"02991-040", long_name:"02991-040", types:{"postal_code"}}}, geometry:{viewport:{northeast:{lat:-23.432391219708, lng:-46.725645019708}, southwest:{lat:-23.435089180292, lng:-46.728342980292}}, location:{lat:-23.4337402, lng:-46.726994}, location_type:"ROOFTOP"}, place_id:"ChIJgVGzRqL7zpQRTQPNqsmBVLY", types:{"street_address"}}}, status:"OK"}

I would like to extract just

02991-040

which appears three times on the returning message,

once as part of the formatted address:

formatted_address:\"Rua Dr. Carmelo D'Agostino, 628 - Jardim Rincão, São Paulo - SP, 02991-040, Brazil

and twice as the postal code

{short_name:"02991-040", long_name:"02991-040", types:{"postal_code"}

I have spent quite some time trying to find it by myself but may be I haven't tried hard / smart enough so would really appreciate any help.

Best

1

1 Answers

0
votes

The script has three variables firstResult, secondResult and thirdResult which will contain the three values you want to extract.

if Resultado's status is "OK" then
    set theData to item 1 of results of Resultado
    set formattedAddress to formatted_address of theData
    set {TID, text item delimiters} to {text item delimiters, ", "}
    set firstResult to text item 4 of formattedAddress
    set text item delimiters to TID
    set components to address_components of theData
    tell last item of components
        set secondResult to its long_name
        set thirdResult to its short_name
    end tell
end if