JSON square bracket - VBA

ericlch16

Active Member
Joined
Nov 2, 2007
Messages
315
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hello experts,

I have using Convert Excel to JSON Using VBA | Excelerator Solutions as a reference.

I have jsonstring = JsonConverter.ConvertToJson(jsonItems)

where jsonitems is a collection

the jsonstring is an array as it starts with a [ and finish with a ].

I need to remove the [ and ] at the start and end of the string. I can do that by using the MID function but manipulating a string is not good coding practice. How can i remove the [ and ] without changing the string? I read i need to parse it but not sure how. any help?

Thanks,
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
If jsonitems is a collection, you can already parse its contents using a loop.
VBA Code:
Dim thing
For Each thing In jsonitems
    Debug.Print thing
Next thing
And if you need a specific item, you can refer to it using its index number.
Debug.Print jsonitems(1)

You use the ConvertToJson method when you need the JSON string that has curly brackets for objects, square brackets for collections, etc. For instance, if you wanted to visualize it, you could even add some whitespaces and it would prettify it to help you read it. You usually utilize that method to send a JSON string as a body for a HTTP POST/PUT request.
 
Last edited:
Upvote 0
If jsonitems is a collection, you can already parse its contents using a loop.
VBA Code:
Dim thing
For Each thing In jsonitems
    Debug.Print thing
Next thing
And if you need a specific item, you can refer to it using its index number.
Debug.Print jsonitems(1)

You use the ConvertToJson method when you need the JSON string that has curly brackets for objects, square brackets for collections, etc. For instance, if you wanted to visualize it, you could even add some whitespaces and it would prettify it to help you read it. You usually utilize that method to send a JSON string as a body for a HTTP POST/PUT request.
the issue is the output of the JsonConverter.ConvertToJson(jsonItems) is an array i.e
[
{"id":1
"name":john
}
]

The API does not like the [ at the beginning and the ] at the end. The question is how to get the output without the square brackets?
 
Upvote 0
I have jsonstring = JsonConverter.ConvertToJson(jsonItems)
The API does not like the [ at the beginning and the ] at the end. The question is how to get the output without the square brackets?

It depends, is the API accepting only one item at a time? then, for example, to send the first object:
jsonstring = JsonConverter.ConvertToJson(jsonItems(1))
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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