Results 1 to 14 of 14

Thread: [RESOLVED] Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Resolved [RESOLVED] Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    I need to make an API call to a url giving it a json string in this format (ignore the line numbers, of course):
    Code:
    1.  {					
    2.             "properties": [					
    3.                 {					
    4.                     "property": "email",					
    5.                     "value": "new-email@hubspot.com"					
    6.                 },					
    7.                 {					
    8.                     "property": "firstname",					
    9.                     "value": "Updated"					
    10.                 },					
    11.                 {					
    12.                     "property": "lastname",					
    13.                     "value": "Lead"					
    14.                 },					
    15.                 {					
    16.                     "property": "website",					
    17.                     "value": "http://hubspot-updated-lead.com"					
    18.                 },					
    19.                 {					
    20.                     "property": "lifecyclestage",					
    21.                     "value": "customer"					
    22.                 }					
    23.             ]					
    24.         }					
    25
    I want to create a class with these properties so I can call a method for each record in our database and plug in the email, firstname, etc. I am having trouble with it because of the repetition of "property" each time. In other words, it doesn't fit the mold of this example I found:
    Code:
    using System;
    using System.Web.Script.Serialization;
    
    public class MyDate
    {
        public int year;
        public int month;
        public int day;
    }
    
    public class Lad
    {
        public string firstName;
        public string lastName;
        public MyDate dateOfBirth;
    }
    
    class Program
    {
        static void Main()
        {
            var obj = new Lad
            {
                firstName = "Markoff",
                lastName = "Chaney",
                dateOfBirth = new MyDate
                {
                    year = 1901,
                    month = 4,
                    day = 30
                }
            };
            var json = new JavaScriptSerializer().Serialize(obj);
            Console.WriteLine(json);
        }
    }
    which produces:
    Code:
    {
        "firstName": "Markoff",
        "lastName": "Chaney",
        "dateOfBirth": {
            "year": 1901,
            "month": 4,
            "day": 30
        }
    }
    Thanks if you can help. I am in a hurry to get this done.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    Never mind that I need a quick answer. I'd still like this answered, but for now because I need it quick I am just building the json field by field in a stringbuilder. So put me low on your list .
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  3. #3
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    A nice tool I found is JSON to C#.

    Paste your JSON in there and it will give you a C# object that matches.

    This is it's recommendation for your problem:

    Create a Property object with a name and value property. Then, have a list of that object.

    Code:
    public class Property
    {
        public string property { get; set; }
        public string value { get; set; }
    }
    
    public class RootObject
    {
        public List<Property> properties { get; set; }
    }
    Last edited by kfcSmitty; Feb 27th, 2014 at 09:16 AM.

  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    Thank you! Gave it a cursory glance, and it looks nice. I love a useful tool!

    BTW, I think you had a typo in your link. It's this, http://json2csharp.com/, not this, http://json2csharp.com/[/url (I clicked your link and got a 404).
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  5. #5
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    Quote Originally Posted by MMock View Post
    Thank you! Gave it a cursory glance, and it looks nice. I love a useful tool!

    BTW, I think you had a typo in your link. It's this, http://json2csharp.com/, not this, http://json2csharp.com/[/url (I clicked your link and got a 404).
    Whoops, fixed now

  6. #6
    New Member
    Join Date
    Feb 2014
    Posts
    5

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    Hi MMock

    .In you previous post i saw you were having issues to post json data in hubspot Api.Right now i am having hard time to for creating a contact.

    I need to post data in their api
    https://developers.hubspot.com/docs/...create_contact


    my c# code(sample)
    public string AddContact()
    {

    List<SRcontact> con = new List<SRcontact>()
    {
    new SRcontact(){ FirstName="Tom", Lastname="a",Email="sda@hotmail.com"},
    new SRcontact(){ FirstName="Lucy", Lastname="b",Email="dd@hotmail.com"}
    };

    string jsonString = SerializeJSon<List<SRcontact>>(con);

    return jsonString;



    }

    do you have any idea what is next step to post that two contact list in their api??? please help me, spending lot of time on this.

    Thank you

  7. #7
    New Member
    Join Date
    Feb 2014
    Posts
    5

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    please help me

  8. #8
    New Member
    Join Date
    Feb 2014
    Posts
    5

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    Hi MMock

    I know you were working on hubspot API.Could you share some of idea how to proceed in my following project

    I need to create a new contact in (https://api.hubapi.com/contacts/v1/c.../?hapikey=demo)
    Please see below code : What is next step to add contact in their api after returning jason format of data.please see below.
    c# code

    public string dataformat()
    {


    List<SRcontact> con = new List<SRcontact>()
    {
    new SRcontact(){ FirstName="Tom", Lastname="a",Email="sda@hotmail.com"},
    new SRcontact(){ FirstName="Lucy", Lastname="b",Email="kc@hotmail.com"}
    };

    string jsonString = SerializeJSon<List<SRcontact>>(con);

    return jsonString;

    }

    Please help me

  9. #9

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    Hi proc#. I would be happy to help you! I think I must've left for the day before you posted but I can definitely help today. You seem to be at the point where I was just a few days ago.

    Go here https://github.com/atniptw/hubspot-api-dotnet and get their HttpUtils.cs class. It has a SendRequestByPost() method and a SendRequestByGet() method. In the project I had started, I created a new folder, called it Utilities, and put this class file in that folder.

    You'll be using this to create a contact: public static string SendRequestByPost(string serviceUrl, string postData, System.Net.WebProxy proxy, int timeout)

    serviceUrl should be "https://api.hubapi.com/contacts/v1/contact?hapikey=YOUR_API_KEY"; (Have you generated a key yet?)
    postData is going to be that jsonString you are building. If you want to reply and post your string, I can tell you if it looks correct. Or try it, and see if the api accepts it.
    You shouldn't have to worry about proxy or timeout. If you call the signature of the Post method with just the serviceURL and the postData that you set, proxy and timeout will default.

    Code:
            public static string SendRequestByPost(string postData)
            {
                return SendRequestByPost(serviceUrl, postData, null, DefaultTimeout);
                
            }
    Let me know how that goes.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  10. #10
    New Member
    Join Date
    Feb 2014
    Posts
    5

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    Hi MMock

    Thanks for your reply.That will help a lot.I will follow the code yo gave me.This is my first c# project in my company.So bit getting hard time. SO far i got up to here.Do you thin i am going right direction. I am using WCF.

    wcf code
    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "json")]
    string AddContact();

    methord calll

    public string AddContact()
    {

    List<SRcontact> con = new List<SRcontact>()
    {
    new SRcontact(){ FirstName="Tom", Lastname="a",Email="sda@hotmail.com"},
    new SRcontact(){ FirstName="Lucy", Lastname="b",Email="kc@hotmail.com"}
    };

    string jsonString = SerializeJSon<List<SRcontact>>(con);
    string WEBSERVICE_URL = "https://api.hubapi.com/contacts/v1/contact/?hapikey=demo";
    StreamWriter requestWriter;
    string ret = string.Empty;

    var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
    if (webRequest != null)
    {
    webRequest.Method = "POST";
    webRequest.Timeout = 20000;
    webRequest.ContentType = "application/json";


    using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
    {
    requestWriter.Write(jsonString);
    }

    }


    HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
    Stream resStream = resp.GetResponseStream();
    StreamReader reader = new StreamReader(resStream);
    ret = reader.ReadToEnd();

    return ret;
    }

  11. #11

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    A couple observations/comments -

    I am a C# programmer but I am new at my company and am working with HubSpot as my first project. I don't use WCF, but I should be able to help you. You can also join the HubSpot developer forum and ask questions there. The tech guy there has been helping me with this first project.

    Your code looks okay. (When you post code to this forum, you can surround it with tags and it will format nicely, for easier readability. There is an icon to generate these tags. It is the # icon in the "toolbar"). I am not sure "demo" will accept your post, but try it. That is why I asked if you had your own API key yet.

    Have you ever used json lint? After you build your json to create the contact, copy and paste it in this validator and it will tell you if it's valid json. HubSpot may still reject it, but at least you'll know the json is good and it's something specific to what HubSpot is expecting that you have to fix. If your json is bad, fix it accordingly.

    I think you're ready to give it a try now. Good luck.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  12. #12
    New Member
    Join Date
    Feb 2014
    Posts
    5

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    MMock

    Thanks for your suggestion.I checked with validator, it is returning valid format.I am working to get key from hub spot.Thanks for your help.I will update you.Long way to go.

  13. #13

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    Quote Originally Posted by kfcSmitty View Post
    A nice tool I found is JSON to C#.

    Paste your JSON in there and it will give you a C# object that matches.
    As I had said, I didn't need this at the time but I tucked it away for future use and am using it now. Just wanted to recommend this and to thank kfcSmitty again - it is a great tool that I've been using all morning with great success, and what a timesaver it is! Thx .
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  14. #14

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Build json string with variable data - PLEASE HELP. NEED ANSWER FAST.

    Quote Originally Posted by kfcSmitty View Post
    A nice tool I found is JSON to C#.

    Paste your JSON in there and it will give you a C# object that matches.
    As I had said, I didn't need this at the time but I tucked it away for future use and am using it now. Just wanted to recommend this and to thank kfcSmitty again - it is a great tool that I've been using all morning with great success, and what a timesaver it is! Thx .
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width