vba json read sub array how to read










0














API using get it bellow json data



[
"Id":"8",
"Date":"12/11/2018",


"readings":["Id":"61","DailyLogBookID":"8","Substation":"MS-1","LoadMax":"898","LoadMin":"9898","VoltageMax":"98","VoltageMin":"98","FrequencyMax":"988","FrequencyMin":"9898","PFMax":"988","PFMin":"989","PowerDip":"9898","Remarks":"9898",

"Id":"62","DailyLogBookID":"8","Substation":"MS-2","LoadMax":"9889","LoadMin":"98","VoltageMax":"98","VoltageMin":"889","FrequencyMax":"9898","FrequencyMin":"98","PFMax":"98","PFMin":"98","PowerDip":"98","Remarks":"98"
]]

/*************VBA Code************/

Sub CallChildDate(id As String)
Dim http As Object, JSON As Object, i, j As Integer
Set http = CreateObject("MSXML2.XMLHTTP")
Dim strUrl As String
http.Open "GET", id, False
http.send
Set JSON = ParseJson(http.responseText)
i = 1

For Each item In JSON
Sheets(1).Cells(i, 1).Value = item("Id")
Sheets(1).Cells(i, 2).Value = item("Date")
i = i + 1
Next

End Sub


How to get it above "readings" data get assign in cell value. how to "readings" array data get for each using assn in cell.










share|improve this question























  • What have you done so far? A good place to start is here: github.com/VBA-tools/VBA-JSON
    – Tim Williams
    Nov 10 at 6:30










  • I have assign above json value assign variable "JSON" For Each Item In JSON For i = 1 To 10 Sheets(1).Cells(i, 1).Value = Item("readings")(i)("Id") Next i Next But Not working, sub json array how to get it.
    – R saravanan
    Nov 10 at 6:44











  • If you have code, edit your question to add it. Code in comments is not very readable. "Not working" also needs some explanation - does it error? If Yes what error do you get?
    – Tim Williams
    Nov 10 at 6:46











  • Hi have change my questions
    – R saravanan
    Nov 10 at 7:08










  • should be easier to connect to the JSON file with Excel Power Query support.office.com/en-us/article/…
    – Slai
    Nov 10 at 8:44















0














API using get it bellow json data



[
"Id":"8",
"Date":"12/11/2018",


"readings":["Id":"61","DailyLogBookID":"8","Substation":"MS-1","LoadMax":"898","LoadMin":"9898","VoltageMax":"98","VoltageMin":"98","FrequencyMax":"988","FrequencyMin":"9898","PFMax":"988","PFMin":"989","PowerDip":"9898","Remarks":"9898",

"Id":"62","DailyLogBookID":"8","Substation":"MS-2","LoadMax":"9889","LoadMin":"98","VoltageMax":"98","VoltageMin":"889","FrequencyMax":"9898","FrequencyMin":"98","PFMax":"98","PFMin":"98","PowerDip":"98","Remarks":"98"
]]

/*************VBA Code************/

Sub CallChildDate(id As String)
Dim http As Object, JSON As Object, i, j As Integer
Set http = CreateObject("MSXML2.XMLHTTP")
Dim strUrl As String
http.Open "GET", id, False
http.send
Set JSON = ParseJson(http.responseText)
i = 1

For Each item In JSON
Sheets(1).Cells(i, 1).Value = item("Id")
Sheets(1).Cells(i, 2).Value = item("Date")
i = i + 1
Next

End Sub


How to get it above "readings" data get assign in cell value. how to "readings" array data get for each using assn in cell.










share|improve this question























  • What have you done so far? A good place to start is here: github.com/VBA-tools/VBA-JSON
    – Tim Williams
    Nov 10 at 6:30










  • I have assign above json value assign variable "JSON" For Each Item In JSON For i = 1 To 10 Sheets(1).Cells(i, 1).Value = Item("readings")(i)("Id") Next i Next But Not working, sub json array how to get it.
    – R saravanan
    Nov 10 at 6:44











  • If you have code, edit your question to add it. Code in comments is not very readable. "Not working" also needs some explanation - does it error? If Yes what error do you get?
    – Tim Williams
    Nov 10 at 6:46











  • Hi have change my questions
    – R saravanan
    Nov 10 at 7:08










  • should be easier to connect to the JSON file with Excel Power Query support.office.com/en-us/article/…
    – Slai
    Nov 10 at 8:44













0












0








0







API using get it bellow json data



[
"Id":"8",
"Date":"12/11/2018",


"readings":["Id":"61","DailyLogBookID":"8","Substation":"MS-1","LoadMax":"898","LoadMin":"9898","VoltageMax":"98","VoltageMin":"98","FrequencyMax":"988","FrequencyMin":"9898","PFMax":"988","PFMin":"989","PowerDip":"9898","Remarks":"9898",

"Id":"62","DailyLogBookID":"8","Substation":"MS-2","LoadMax":"9889","LoadMin":"98","VoltageMax":"98","VoltageMin":"889","FrequencyMax":"9898","FrequencyMin":"98","PFMax":"98","PFMin":"98","PowerDip":"98","Remarks":"98"
]]

/*************VBA Code************/

Sub CallChildDate(id As String)
Dim http As Object, JSON As Object, i, j As Integer
Set http = CreateObject("MSXML2.XMLHTTP")
Dim strUrl As String
http.Open "GET", id, False
http.send
Set JSON = ParseJson(http.responseText)
i = 1

For Each item In JSON
Sheets(1).Cells(i, 1).Value = item("Id")
Sheets(1).Cells(i, 2).Value = item("Date")
i = i + 1
Next

End Sub


How to get it above "readings" data get assign in cell value. how to "readings" array data get for each using assn in cell.










share|improve this question















API using get it bellow json data



[
"Id":"8",
"Date":"12/11/2018",


"readings":["Id":"61","DailyLogBookID":"8","Substation":"MS-1","LoadMax":"898","LoadMin":"9898","VoltageMax":"98","VoltageMin":"98","FrequencyMax":"988","FrequencyMin":"9898","PFMax":"988","PFMin":"989","PowerDip":"9898","Remarks":"9898",

"Id":"62","DailyLogBookID":"8","Substation":"MS-2","LoadMax":"9889","LoadMin":"98","VoltageMax":"98","VoltageMin":"889","FrequencyMax":"9898","FrequencyMin":"98","PFMax":"98","PFMin":"98","PowerDip":"98","Remarks":"98"
]]

/*************VBA Code************/

Sub CallChildDate(id As String)
Dim http As Object, JSON As Object, i, j As Integer
Set http = CreateObject("MSXML2.XMLHTTP")
Dim strUrl As String
http.Open "GET", id, False
http.send
Set JSON = ParseJson(http.responseText)
i = 1

For Each item In JSON
Sheets(1).Cells(i, 1).Value = item("Id")
Sheets(1).Cells(i, 2).Value = item("Date")
i = i + 1
Next

End Sub


How to get it above "readings" data get assign in cell value. how to "readings" array data get for each using assn in cell.







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 7:19









Pᴇʜ

20.2k42650




20.2k42650










asked Nov 10 at 6:27









R saravanan

33




33











  • What have you done so far? A good place to start is here: github.com/VBA-tools/VBA-JSON
    – Tim Williams
    Nov 10 at 6:30










  • I have assign above json value assign variable "JSON" For Each Item In JSON For i = 1 To 10 Sheets(1).Cells(i, 1).Value = Item("readings")(i)("Id") Next i Next But Not working, sub json array how to get it.
    – R saravanan
    Nov 10 at 6:44











  • If you have code, edit your question to add it. Code in comments is not very readable. "Not working" also needs some explanation - does it error? If Yes what error do you get?
    – Tim Williams
    Nov 10 at 6:46











  • Hi have change my questions
    – R saravanan
    Nov 10 at 7:08










  • should be easier to connect to the JSON file with Excel Power Query support.office.com/en-us/article/…
    – Slai
    Nov 10 at 8:44
















  • What have you done so far? A good place to start is here: github.com/VBA-tools/VBA-JSON
    – Tim Williams
    Nov 10 at 6:30










  • I have assign above json value assign variable "JSON" For Each Item In JSON For i = 1 To 10 Sheets(1).Cells(i, 1).Value = Item("readings")(i)("Id") Next i Next But Not working, sub json array how to get it.
    – R saravanan
    Nov 10 at 6:44











  • If you have code, edit your question to add it. Code in comments is not very readable. "Not working" also needs some explanation - does it error? If Yes what error do you get?
    – Tim Williams
    Nov 10 at 6:46











  • Hi have change my questions
    – R saravanan
    Nov 10 at 7:08










  • should be easier to connect to the JSON file with Excel Power Query support.office.com/en-us/article/…
    – Slai
    Nov 10 at 8:44















What have you done so far? A good place to start is here: github.com/VBA-tools/VBA-JSON
– Tim Williams
Nov 10 at 6:30




What have you done so far? A good place to start is here: github.com/VBA-tools/VBA-JSON
– Tim Williams
Nov 10 at 6:30












I have assign above json value assign variable "JSON" For Each Item In JSON For i = 1 To 10 Sheets(1).Cells(i, 1).Value = Item("readings")(i)("Id") Next i Next But Not working, sub json array how to get it.
– R saravanan
Nov 10 at 6:44





I have assign above json value assign variable "JSON" For Each Item In JSON For i = 1 To 10 Sheets(1).Cells(i, 1).Value = Item("readings")(i)("Id") Next i Next But Not working, sub json array how to get it.
– R saravanan
Nov 10 at 6:44













If you have code, edit your question to add it. Code in comments is not very readable. "Not working" also needs some explanation - does it error? If Yes what error do you get?
– Tim Williams
Nov 10 at 6:46





If you have code, edit your question to add it. Code in comments is not very readable. "Not working" also needs some explanation - does it error? If Yes what error do you get?
– Tim Williams
Nov 10 at 6:46













Hi have change my questions
– R saravanan
Nov 10 at 7:08




Hi have change my questions
– R saravanan
Nov 10 at 7:08












should be easier to connect to the JSON file with Excel Power Query support.office.com/en-us/article/…
– Slai
Nov 10 at 8:44




should be easier to connect to the JSON file with Excel Power Query support.office.com/en-us/article/…
– Slai
Nov 10 at 8:44












1 Answer
1






active

oldest

votes


















0














I note you have now edited the JSON and the question somewhat:



From your prior question: You were almost there. Below I am reading the JSON in from a file.



The { means a dictionary so you access by key, the [ means a collection so you access by index. "" means a string literal so you read as is. I use a JSON parser to handle the JSON read in from file. After adding the .bas from that link to your project you then add a reference via VBE > Tools > References > Microsoft Scripting Runtime.



If you examine the JSON structure to see where the ids within readings are you will see the initial object is a collection. readings is in the first item of that collection which is a dictionary. readings is the key that returns an collection of dictionaries:



Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 


So, you are looping over a collection of dictionaries in the code below where each item is a dictionary. You can the loop the dictionary keys and access and use the key (e.g. id) for each dictionary within to retrieve the associated value.





Option Explicit

Public Sub ReadValues()
Dim fso As Object, stream As Object, jsonText As String, item As Object
Dim json As Object, ws As Worksheet, i As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = fso.OpenTextFile("C:UsersHarrisQDesktoptest.json", ForReading)
jsonText = stream.ReadAll
stream.Close

Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 'Collection of dictionaries

Dim c As Long, key As Variant, arr(), r As Long

With ws
arr() = json(1).keys
.Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
r = 2
For Each item In json
c = 1
For Each key In item
.Cells(r, c).Value = item(key)
c = c + 1
Next
r = r + 1
Next
End With
End Sub



With your revised question:



Option Explicit
Public Sub CallChildDate(id As String)
Dim json As Object, item As Object, ws As Worksheet, i As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")

With CreateObject("MSXML2.XMLHTTP")
.Open "GET", id, False '<==Assuming id is actually a URL and not to be concantenated with your former variable strURL.
.send
Set json = JsonConverter.ParseJson(.responseText)(1)("readings") 'Collection of dictionaries
End With

Dim c As Long, key As Variant, arr(), r As Long

With ws
arr() = json(1).keys
.Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
r = 2
For Each item In json
c = 1
For Each key In item
.Cells(r, c).Value = item(key)
c = c + 1
Next
r = r + 1
Next
End With
End Sub



Output using your initial JSON string:



enter image description here






share|improve this answer






















  • i am not working in json file i'm working in api data get abow json data your code not working. "ParseJson(jsonText)(1)("readings")" Error now.
    – R saravanan
    Nov 10 at 9:28










  • how to get in sub array is more then array list. how to working now.
    – R saravanan
    Nov 10 at 9:30











  • You haven't included the rest of your code so I have had to read your JSON from file. You would have a line that read something like Set json = JsonConverter.ParseJson(.responseText)(1)("readings") or xmlhttpobject.responseText (what ever variable is holding the xmlhttp object if using XHR request.)
    – QHarr
    Nov 10 at 9:42











  • how to get in sub array is more then array list. how to working now what does that mean? My code writes all the ids out to the sheet. To get every item in the inner dictionaries simply loop all the keys.
    – QHarr
    Nov 10 at 9:43











  • See edits to answer.
    – QHarr
    Nov 10 at 10:03










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236528%2fvba-json-read-sub-array-how-to-read%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I note you have now edited the JSON and the question somewhat:



From your prior question: You were almost there. Below I am reading the JSON in from a file.



The { means a dictionary so you access by key, the [ means a collection so you access by index. "" means a string literal so you read as is. I use a JSON parser to handle the JSON read in from file. After adding the .bas from that link to your project you then add a reference via VBE > Tools > References > Microsoft Scripting Runtime.



If you examine the JSON structure to see where the ids within readings are you will see the initial object is a collection. readings is in the first item of that collection which is a dictionary. readings is the key that returns an collection of dictionaries:



Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 


So, you are looping over a collection of dictionaries in the code below where each item is a dictionary. You can the loop the dictionary keys and access and use the key (e.g. id) for each dictionary within to retrieve the associated value.





Option Explicit

Public Sub ReadValues()
Dim fso As Object, stream As Object, jsonText As String, item As Object
Dim json As Object, ws As Worksheet, i As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = fso.OpenTextFile("C:UsersHarrisQDesktoptest.json", ForReading)
jsonText = stream.ReadAll
stream.Close

Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 'Collection of dictionaries

Dim c As Long, key As Variant, arr(), r As Long

With ws
arr() = json(1).keys
.Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
r = 2
For Each item In json
c = 1
For Each key In item
.Cells(r, c).Value = item(key)
c = c + 1
Next
r = r + 1
Next
End With
End Sub



With your revised question:



Option Explicit
Public Sub CallChildDate(id As String)
Dim json As Object, item As Object, ws As Worksheet, i As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")

With CreateObject("MSXML2.XMLHTTP")
.Open "GET", id, False '<==Assuming id is actually a URL and not to be concantenated with your former variable strURL.
.send
Set json = JsonConverter.ParseJson(.responseText)(1)("readings") 'Collection of dictionaries
End With

Dim c As Long, key As Variant, arr(), r As Long

With ws
arr() = json(1).keys
.Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
r = 2
For Each item In json
c = 1
For Each key In item
.Cells(r, c).Value = item(key)
c = c + 1
Next
r = r + 1
Next
End With
End Sub



Output using your initial JSON string:



enter image description here






share|improve this answer






















  • i am not working in json file i'm working in api data get abow json data your code not working. "ParseJson(jsonText)(1)("readings")" Error now.
    – R saravanan
    Nov 10 at 9:28










  • how to get in sub array is more then array list. how to working now.
    – R saravanan
    Nov 10 at 9:30











  • You haven't included the rest of your code so I have had to read your JSON from file. You would have a line that read something like Set json = JsonConverter.ParseJson(.responseText)(1)("readings") or xmlhttpobject.responseText (what ever variable is holding the xmlhttp object if using XHR request.)
    – QHarr
    Nov 10 at 9:42











  • how to get in sub array is more then array list. how to working now what does that mean? My code writes all the ids out to the sheet. To get every item in the inner dictionaries simply loop all the keys.
    – QHarr
    Nov 10 at 9:43











  • See edits to answer.
    – QHarr
    Nov 10 at 10:03















0














I note you have now edited the JSON and the question somewhat:



From your prior question: You were almost there. Below I am reading the JSON in from a file.



The { means a dictionary so you access by key, the [ means a collection so you access by index. "" means a string literal so you read as is. I use a JSON parser to handle the JSON read in from file. After adding the .bas from that link to your project you then add a reference via VBE > Tools > References > Microsoft Scripting Runtime.



If you examine the JSON structure to see where the ids within readings are you will see the initial object is a collection. readings is in the first item of that collection which is a dictionary. readings is the key that returns an collection of dictionaries:



Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 


So, you are looping over a collection of dictionaries in the code below where each item is a dictionary. You can the loop the dictionary keys and access and use the key (e.g. id) for each dictionary within to retrieve the associated value.





Option Explicit

Public Sub ReadValues()
Dim fso As Object, stream As Object, jsonText As String, item As Object
Dim json As Object, ws As Worksheet, i As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = fso.OpenTextFile("C:UsersHarrisQDesktoptest.json", ForReading)
jsonText = stream.ReadAll
stream.Close

Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 'Collection of dictionaries

Dim c As Long, key As Variant, arr(), r As Long

With ws
arr() = json(1).keys
.Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
r = 2
For Each item In json
c = 1
For Each key In item
.Cells(r, c).Value = item(key)
c = c + 1
Next
r = r + 1
Next
End With
End Sub



With your revised question:



Option Explicit
Public Sub CallChildDate(id As String)
Dim json As Object, item As Object, ws As Worksheet, i As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")

With CreateObject("MSXML2.XMLHTTP")
.Open "GET", id, False '<==Assuming id is actually a URL and not to be concantenated with your former variable strURL.
.send
Set json = JsonConverter.ParseJson(.responseText)(1)("readings") 'Collection of dictionaries
End With

Dim c As Long, key As Variant, arr(), r As Long

With ws
arr() = json(1).keys
.Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
r = 2
For Each item In json
c = 1
For Each key In item
.Cells(r, c).Value = item(key)
c = c + 1
Next
r = r + 1
Next
End With
End Sub



Output using your initial JSON string:



enter image description here






share|improve this answer






















  • i am not working in json file i'm working in api data get abow json data your code not working. "ParseJson(jsonText)(1)("readings")" Error now.
    – R saravanan
    Nov 10 at 9:28










  • how to get in sub array is more then array list. how to working now.
    – R saravanan
    Nov 10 at 9:30











  • You haven't included the rest of your code so I have had to read your JSON from file. You would have a line that read something like Set json = JsonConverter.ParseJson(.responseText)(1)("readings") or xmlhttpobject.responseText (what ever variable is holding the xmlhttp object if using XHR request.)
    – QHarr
    Nov 10 at 9:42











  • how to get in sub array is more then array list. how to working now what does that mean? My code writes all the ids out to the sheet. To get every item in the inner dictionaries simply loop all the keys.
    – QHarr
    Nov 10 at 9:43











  • See edits to answer.
    – QHarr
    Nov 10 at 10:03













0












0








0






I note you have now edited the JSON and the question somewhat:



From your prior question: You were almost there. Below I am reading the JSON in from a file.



The { means a dictionary so you access by key, the [ means a collection so you access by index. "" means a string literal so you read as is. I use a JSON parser to handle the JSON read in from file. After adding the .bas from that link to your project you then add a reference via VBE > Tools > References > Microsoft Scripting Runtime.



If you examine the JSON structure to see where the ids within readings are you will see the initial object is a collection. readings is in the first item of that collection which is a dictionary. readings is the key that returns an collection of dictionaries:



Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 


So, you are looping over a collection of dictionaries in the code below where each item is a dictionary. You can the loop the dictionary keys and access and use the key (e.g. id) for each dictionary within to retrieve the associated value.





Option Explicit

Public Sub ReadValues()
Dim fso As Object, stream As Object, jsonText As String, item As Object
Dim json As Object, ws As Worksheet, i As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = fso.OpenTextFile("C:UsersHarrisQDesktoptest.json", ForReading)
jsonText = stream.ReadAll
stream.Close

Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 'Collection of dictionaries

Dim c As Long, key As Variant, arr(), r As Long

With ws
arr() = json(1).keys
.Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
r = 2
For Each item In json
c = 1
For Each key In item
.Cells(r, c).Value = item(key)
c = c + 1
Next
r = r + 1
Next
End With
End Sub



With your revised question:



Option Explicit
Public Sub CallChildDate(id As String)
Dim json As Object, item As Object, ws As Worksheet, i As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")

With CreateObject("MSXML2.XMLHTTP")
.Open "GET", id, False '<==Assuming id is actually a URL and not to be concantenated with your former variable strURL.
.send
Set json = JsonConverter.ParseJson(.responseText)(1)("readings") 'Collection of dictionaries
End With

Dim c As Long, key As Variant, arr(), r As Long

With ws
arr() = json(1).keys
.Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
r = 2
For Each item In json
c = 1
For Each key In item
.Cells(r, c).Value = item(key)
c = c + 1
Next
r = r + 1
Next
End With
End Sub



Output using your initial JSON string:



enter image description here






share|improve this answer














I note you have now edited the JSON and the question somewhat:



From your prior question: You were almost there. Below I am reading the JSON in from a file.



The { means a dictionary so you access by key, the [ means a collection so you access by index. "" means a string literal so you read as is. I use a JSON parser to handle the JSON read in from file. After adding the .bas from that link to your project you then add a reference via VBE > Tools > References > Microsoft Scripting Runtime.



If you examine the JSON structure to see where the ids within readings are you will see the initial object is a collection. readings is in the first item of that collection which is a dictionary. readings is the key that returns an collection of dictionaries:



Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 


So, you are looping over a collection of dictionaries in the code below where each item is a dictionary. You can the loop the dictionary keys and access and use the key (e.g. id) for each dictionary within to retrieve the associated value.





Option Explicit

Public Sub ReadValues()
Dim fso As Object, stream As Object, jsonText As String, item As Object
Dim json As Object, ws As Worksheet, i As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = fso.OpenTextFile("C:UsersHarrisQDesktoptest.json", ForReading)
jsonText = stream.ReadAll
stream.Close

Set json = JsonConverter.ParseJson(jsonText)(1)("readings") 'Collection of dictionaries

Dim c As Long, key As Variant, arr(), r As Long

With ws
arr() = json(1).keys
.Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
r = 2
For Each item In json
c = 1
For Each key In item
.Cells(r, c).Value = item(key)
c = c + 1
Next
r = r + 1
Next
End With
End Sub



With your revised question:



Option Explicit
Public Sub CallChildDate(id As String)
Dim json As Object, item As Object, ws As Worksheet, i As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")

With CreateObject("MSXML2.XMLHTTP")
.Open "GET", id, False '<==Assuming id is actually a URL and not to be concantenated with your former variable strURL.
.send
Set json = JsonConverter.ParseJson(.responseText)(1)("readings") 'Collection of dictionaries
End With

Dim c As Long, key As Variant, arr(), r As Long

With ws
arr() = json(1).keys
.Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
r = 2
For Each item In json
c = 1
For Each key In item
.Cells(r, c).Value = item(key)
c = c + 1
Next
r = r + 1
Next
End With
End Sub



Output using your initial JSON string:



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 10:02

























answered Nov 10 at 7:52









QHarr

29.9k81841




29.9k81841











  • i am not working in json file i'm working in api data get abow json data your code not working. "ParseJson(jsonText)(1)("readings")" Error now.
    – R saravanan
    Nov 10 at 9:28










  • how to get in sub array is more then array list. how to working now.
    – R saravanan
    Nov 10 at 9:30











  • You haven't included the rest of your code so I have had to read your JSON from file. You would have a line that read something like Set json = JsonConverter.ParseJson(.responseText)(1)("readings") or xmlhttpobject.responseText (what ever variable is holding the xmlhttp object if using XHR request.)
    – QHarr
    Nov 10 at 9:42











  • how to get in sub array is more then array list. how to working now what does that mean? My code writes all the ids out to the sheet. To get every item in the inner dictionaries simply loop all the keys.
    – QHarr
    Nov 10 at 9:43











  • See edits to answer.
    – QHarr
    Nov 10 at 10:03
















  • i am not working in json file i'm working in api data get abow json data your code not working. "ParseJson(jsonText)(1)("readings")" Error now.
    – R saravanan
    Nov 10 at 9:28










  • how to get in sub array is more then array list. how to working now.
    – R saravanan
    Nov 10 at 9:30











  • You haven't included the rest of your code so I have had to read your JSON from file. You would have a line that read something like Set json = JsonConverter.ParseJson(.responseText)(1)("readings") or xmlhttpobject.responseText (what ever variable is holding the xmlhttp object if using XHR request.)
    – QHarr
    Nov 10 at 9:42











  • how to get in sub array is more then array list. how to working now what does that mean? My code writes all the ids out to the sheet. To get every item in the inner dictionaries simply loop all the keys.
    – QHarr
    Nov 10 at 9:43











  • See edits to answer.
    – QHarr
    Nov 10 at 10:03















i am not working in json file i'm working in api data get abow json data your code not working. "ParseJson(jsonText)(1)("readings")" Error now.
– R saravanan
Nov 10 at 9:28




i am not working in json file i'm working in api data get abow json data your code not working. "ParseJson(jsonText)(1)("readings")" Error now.
– R saravanan
Nov 10 at 9:28












how to get in sub array is more then array list. how to working now.
– R saravanan
Nov 10 at 9:30





how to get in sub array is more then array list. how to working now.
– R saravanan
Nov 10 at 9:30













You haven't included the rest of your code so I have had to read your JSON from file. You would have a line that read something like Set json = JsonConverter.ParseJson(.responseText)(1)("readings") or xmlhttpobject.responseText (what ever variable is holding the xmlhttp object if using XHR request.)
– QHarr
Nov 10 at 9:42





You haven't included the rest of your code so I have had to read your JSON from file. You would have a line that read something like Set json = JsonConverter.ParseJson(.responseText)(1)("readings") or xmlhttpobject.responseText (what ever variable is holding the xmlhttp object if using XHR request.)
– QHarr
Nov 10 at 9:42













how to get in sub array is more then array list. how to working now what does that mean? My code writes all the ids out to the sheet. To get every item in the inner dictionaries simply loop all the keys.
– QHarr
Nov 10 at 9:43





how to get in sub array is more then array list. how to working now what does that mean? My code writes all the ids out to the sheet. To get every item in the inner dictionaries simply loop all the keys.
– QHarr
Nov 10 at 9:43













See edits to answer.
– QHarr
Nov 10 at 10:03




See edits to answer.
– QHarr
Nov 10 at 10:03

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236528%2fvba-json-read-sub-array-how-to-read%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế