3

I just have this string:

dim stringa as string

stringa="{"data":{"GET:oauth.openapi.it\/counters":{"counter":22,"paid":0,"limit":false},"GET:oauth.openapi.it\/scopes":{"counter":6,"paid":0,"limit":false},"POST:oauth.openapi.it\/token":{"counter":1,"paid":0,"limit":false},"GET:imprese.openapi.it\/advance":{"counter":14,"paid":0,"limit":false},"GET:imprese.openapi.it\/base":{"counter":2,"paid":0,"limit":false}},"success":true,"message":"","error":null}"

It is all on only one line.

Is it possible to indent and have to the end the typical Json format, similar to:

{
"data":{
    "GET:oauth.openapi.it/counters":{
        "counter":22,
        "paid":0,
        "limit":false
    },
    "GET:oauth.openapi.it/scopes":{
        "counter":6,
        "paid":0,
        "limit":false
    },
    "POST:oauth.openapi.it/token":{
        "counter":1,
        "paid":0,
        "limit":false
    },
    "GET:imprese.openapi.it/advance":{
        "counter":14,
        "paid":0,
        "limit":false
    },
    "GET:imprese.openapi.it/base":{
        "counter":2,
        "paid":0,
        "limit":false
    }
},
"success":true,
"message":"",
"error":null
}

In this case I can see better the arrangement of the nodes.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
user1579247
  • 107
  • 1
  • 6
  • 1
    VBA or VB6? Those are different things. – FunThomas Jun 16 '23 at 09:22
  • sorry, in effect i need in vb6. – user1579247 Jun 16 '23 at 09:47
  • Use one of the JSON libraries mentioned in this question [Is There a JSON Parser for VB6 / VBA?](https://stackoverflow.com/questions/2782076/is-there-a-json-parser-for-vb6-vba) to parse your string and serialize it back to a string. They should have options to create the respective indention – derpirscher Jun 16 '23 at 11:26

1 Answers1

4

You can use JsonDump function like this:

Option Explicit

Private Sub Form_Load()
    Dim stringa As String
    stringa = "{""data"":{""GET:oauth.openapi.it\/counters"":{""counter"":22,""paid"":0,""limit"":false},""GET:oauth.openapi.it\/scopes"":{""counter"":6,""paid"":0,""limit"":false},""POST:oauth.openapi.it\/token"":{""counter"":1,""paid"":0,""limit"":false},""GET:imprese.openapi.it\/advance"":{""counter"":14,""paid"":0,""limit"":false},""GET:imprese.openapi.it\/base"":{""counter"":2,""paid"":0,""limit"":false}},""success"":true,""message"":"""",""error"":null}"
    
    Dim oJson As Object
    Set oJson = JsonParseObject(stringa)
    Debug.Print JsonDump(oJson, MaxWidth:=0)
End Sub

Immediate Window

{
    "data": {
        "GET:oauth.openapi.it/counters": {
            "counter": 22,
            "paid": 0,
            "limit": false
        },
        "GET:oauth.openapi.it/scopes": {
            "counter": 6,
            "paid": 0,
            "limit": false
        },
        "POST:oauth.openapi.it/token": {
            "counter": 1,
            "paid": 0,
            "limit": false
        },
        "GET:imprese.openapi.it/advance": {
            "counter": 14,
            "paid": 0,
            "limit": false
        },
        "GET:imprese.openapi.it/base": {
            "counter": 2,
            "paid": 0,
            "limit": false
        }
    },
    "success": true,
    "message": "",
    "error": null
}

You'll need mdJson.bas included in your project first.

There are other parameters to JsonDump besides MaxWidth which affect output too.

wqw
  • 11,771
  • 1
  • 33
  • 41
  • TKS bro, work perfect! But possible to save the new result in c:\mydir\new_jon.txt – user1579247 Jun 17 '23 at 16:05
  • 2
    @user1579247 Luca! Bro! This is super easy novice task and you've been asking these questions for years now! Try to do it on your own or search vbforums.com on something like "how to write a text file with VB6". – wqw Jun 17 '23 at 16:21
  • TKS!!! OK saved me – user1579247 Jun 17 '23 at 16:41