0

I have a json request from an api in excel and I would like to convert it.

This is what I get:

{data:[{price: 3, amount:7},{price: 21, amount:16},{price: 18, amount:4}]}

I would like to convert this into:

{data:[[3,7],[21,16],[18,4]]}

How can I handle this ?

What I'm doing is here sorry about that:

text = DownloadTextFile("www../api/etc")
Set json = JsonConverter.ParseJson(text)

Then I would like to loop through;

For j = 0 To 100
Range("h" & j) = json(data)(j)(1)
Next j

But Range("h" & j) = json(data)(j)(1) of course does not work, as it is not an array. That is why I would like to convert it. I don't know how can I loop through the hash and get price and amount data.

I will use the library and write here in case I encounter with a problem, thank you!

1
  • 2
    The library most-frequently recommended here is this: github.com/VBA-tools/VBA-JSON. Try that, and post back with code if you run into problems. Commented Jan 12, 2018 at 19:56

1 Answer 1

1
Sub Tester()

    Dim newJ, col, col2
    Dim j, o

    Set j = JsonConverter.ParseJson( _
       "{'data':[{'price': 3, 'amount':7},{'price': 21, 'amount':16},{'price': 18, 'amount':4}]}")

    Set col = New Collection

    For Each o In j("data")
        Set col2 = New Collection
        col2.Add o("price")
        col2.Add o("amount")
        col.Add col2
    Next o

    Set newJ = CreateObject("scripting.dictionary")
    newJ.Add "data", col

    Debug.Print JsonConverter.ConvertToJson(newJ)
    '>> {"data":[[3,7],[21,16],[18,4]]}

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.