cJobject output

52 views
Skip to first unread message

Keith Rossman

unread,
Aug 22, 2013, 7:42:05 PM8/22/13
to excel-r...@googlegroups.com
Hello,

I have a bit of a question about the cJobject.  I am trying to put together a JSON for the mandrill email application.  Here is an example of the intended output:

Enter code here...{
   
"key": "xxxxxxxxxxxxx",
   
"template_name": "30-july-daily-report",
   
"template_content": [
       
   
],
   
"message": {
       
"html": null,
       
"from_name": "Example Name",
       
"to": [
           
{
               
"email": "ke...@deckmonitoring.com",
               
"name": "keith"
           
}
       
],
       
"headers": {
           
"Reply-To": "pdp@deckmonitoring"
       
},
       
"important": false,
       
"merge": true,
       
"global_merge_vars": [
           
{

           
}
       
],
       
"merge_vars": [
           
{
               
"rcpt": "ke...@deckmonitoring.com",
               
"vars": [
                   
{
                   
"name":"COMPANY",
                   
"content": "ABC Motors"
                   
},
                   
{
                   
"name" : "GENDATE",
                   
"content" : "7/27/2013"
                   
}                    
                   
}
               
]
           
}
       
],
       
"tags": [
           
"password-resets"
       
]
   
}
}


I have been following along with the examples on the site, but I am missing something, specifically when it comes to printing the names for sub-structs.  Here is a snippet of my code:

Public Sub testMandrillMailCJ()

Dim Jemail As New cJobject

Dim sTempl, sFromE, sFromN As String

sTempl = "Charity"
sFromE = "ke...@deckmonitoring.com"
sFromN = "Keith"

With Jemail.init(Nothing)
    .add "key", "qJulWGSl5DioRmUiNWId_w"
    .add "template_name", sTempl
    .add("template_content").addArray
    .add "tags"
    With .add("message")
        .add "from_email", 10
        .add "from_name", sFromN
        .add("to").addArray
        .add "track_opens", "true"
        .add "auto_text", "true"
        With .init(Nothing).add("merge_vars").addArray
            .add "rcpt", semailTo
            With .add("vars").addArray
                .add("vars").addArray
            End With
        End With
    End With
End With


Debug.Print Jemail.stringify

Jemail.tearDown
       
End Sub

and the output:
{
    "key": "qJulWGSl5DioRmUiNWId_w",
    "template_name": "Charity",
    "template_content": [{
       
    }],
    "tags": null,
    {
        "merge_vars": [null,
        "vars": [[]]]
    }
}

I am sure there is something simple and syntactic that I am missing, but I can't seem to find it.

Any insight would be appreciated.

Bruce Mcpherson

unread,
Aug 23, 2013, 4:20:06 AM8/23/13
to excel-r...@googlegroups.com
hi keith 
i see 2 problems
- array population
- use of .init with (nothing) as the parent in the middle of an object.  (you would never need to do this)

in javascript, an array is just really an object. and an array element can be a value or another object, so for populating arrays with values only you need to go

with .add("arrayname").addarray
  .add ,value1
  .add, value2
end with

however if you are adding an object as an array element, you need to both add an array element, and then the object itself.. like this. normally of course you'd be dealing with arrays in some kind of loop

with .add("arrayname").addarray
  with .add 
     .add prop1,value1
     .add prop2,value2
  end with
end with


so ive added a few extra things to complete the picture in your example, and get this json

{
    "key": "qJulWGSl5DioRmUiNWId_w",
    "template_name": "Charity",
    "template_content": [
        "abc",
        "xyz"
    ],
    "tags": [
        "x",
        "y"
    ],
    "message": {
        "from_email": 10,
        "from_name": "Keith",
        "to": [
            {
                "email": "x@y.z",
                "name": "x"
            },
            {
                "email": "z@z.z",
                "name": "z"
            }
        ],
        "track_opens": "true",
        "auto_text": "true",
        "merge_vars": [
            {
                "rcpt": null,
                "vars": [
                    {
                        "company": "a",
                        "content": "ca"
                    },
                    {
                        "company": "b",
                        "content": "cb"
                    }
                ]
            }
        ]
    }
}

and the code is




Public Sub testMandrillMailCJ()

Dim Jemail As New cJobject

Dim sTempl, sFromE, sFromN As String

sTempl = "Charity"
sFromN = "Keith"



With Jemail.init(Nothing)
    .add "key", "qJulWGSl5DioRmUiNWId_w"
    .add "template_name", sTempl
    With .add("template_content").addArray
        .add , "abc"
        .add , "xyz"
    End With
    With .add("tags").addArray
        .add , "x"
        .add , "y"
    End With
    With .add("message")
        .add "from_email", 10
        .add "from_name", sFromN
        With .add("to").addArray
            With .add
                .add "email", "x@y.z"
                .add "name", "x"
            End With
            With .add
                .add "email", "z@z.z"
                .add "name", "z"
            End With
        End With
        .add "track_opens", "true"
        .add "auto_text", "true"
        With .add("merge_vars").addArray
            With .add
                .add "rcpt", semailTo
                With .add("vars").addArray
                    With .add
                        .add "company", "a"
                        .add "content", "ca"
                    End With
                    With .add
                        .add "company", "b"
                        .add "content", "cb"
                    End With
                End With
            End With
        End With
    End With
End With


Debug.Print Jemail.stringify(True)

Jemail.tearDown
        
End Sub



--
You received this message because you are subscribed to the Google Groups "Excel Liberation" group.
To unsubscribe from this group and stop receiving emails from it, send an email to excel-ramblin...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Keith Rossman

unread,
Aug 28, 2013, 6:16:33 PM8/28/13
to excel-r...@googlegroups.com
Excellent, thank you Bruce.

What if I wanted to make things a bit more modular, so that the merge_vars array could be defined elsewhere as its own cjObject and insert.  Is there a function to embed a cjObject within a cjObject?

Bruce McPherson

unread,
Aug 28, 2013, 6:30:00 PM8/28/13
to excel-r...@googlegroups.com
Yes there is both an append and a merge method. But the best way is to pass the cjobject as an argument and add to it

Bruce

Sent from my iPad
Reply all
Reply to author
Forward
0 new messages