How to get JSON result into a cell?

varcorb

New Member
Joined
Jun 10, 2014
Messages
32
Hello,

I have a JSON URL "https://maps.googleapis.com/maps/api/distancematrix/json?origins=Aurora+CO&destinations=Willis+TX&mode=driving&language=en_US" That Results Like
Code:
{   "destination_addresses" : [ "Willis, TX, USA" ],   "origin_addresses" : [ "Aurora, CO, USA" ],   "rows" : [      {         "elements" : [            {               "distance" : {                  "text" : "1,577 km",                  "value" : 1577246               },               "duration" : {                  "text" : "14 hours 22 mins",                  "value" : 51716               },               "status" : "OK"            }         ]      }   ],   "status" : "OK"}

I am trying to get the Distance Text into a Cell? How can i do that in Excel?

Thank You for all of your help!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi

This is not a general solution. This code parses directly the JSON string assuming it has the structure like you posted and extracts the value into Sheet1!A2:

Code:
Sub Get_JSON_Goals()
Dim regex As RegExp, regexMatches As MatchCollection
Dim sJSON As String
Dim rOut As Range

' read the JSON text into a string
sJSON = Get_JSON_Text

' get rid of newline characters
sJSON = Replace(sJSON, vbLf, " ")

Set rOut = Worksheets("Sheet1").Range("B1")

Set regex = New RegExp
With regex
    .Pattern = """distance""\s*:\s*{\s*""text""\s*:\s*""([^""]*)"""
    Set regexMatches = .Execute(sJSON)

    ' if no goals are found exit
    If regexMatches.Count = 0 Then
        MsgBox "Not found"
        Exit Sub
    End If

    ' output write distance text
    rOut.Value = regexMatches(0).SubMatches(0)

End With
End Sub


Function Get_JSON_Text() As String
Dim IXMLHTTPReq As MSXML2.xmlhttp

Set IXMLHTTPReq = New MSXML2.xmlhttp

With IXMLHTTPReq
    .Open "GET", "https://maps.googleapis.com/maps/api/distancematrix/json?origins=Aurora+CO&destinations=Willis+TX&mode=driving&language=en_US", False
    .send
    Get_JSON_Text = .responseText
End With
    
End Function
 
Upvote 0

Forum statistics

Threads
1,214,402
Messages
6,119,299
Members
448,885
Latest member
LokiSonic

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top