0

I have to submit this JSON exactly or it does not work. While I was able to create most of it with the help of another member I cannot find information on dealing with nulls. When it submits the null value it will not run through the api.

Here is the JSON I must create

    {
            "resourceType": "Bundle",
            "type": "message",
            "entry": [
            {
                            "resource": {
                                            "resourceType": "MessageHeader",
                                            "timestamp": "2016-12-29T08:00:00-07:00",
                                            "id": "Test1",
                                            "event":
                                                            { "code": "diagnosticreport-provide" },
                                            "source":
                                                            { "endpoint": "http://yourdomain.com/api" },
                                            "destination": [
                                                            { "endpoint": "https://api.com/api/$process-message" }
                                            ]
                            }
            },
            {
                            "resource": {
                                            "resourceType" : "DiagnosticReport",
                                            "extension" : [{
                                                            "url" : "DiagnosticReportDefinition",
                                                            "extension" : [
                                                                            { "url" : "useNewMedications", "valueBoolean": "false" },
                                                                            { "url" : "providePDFReport", "valueBoolean": "false" },
                                                                            { "url" : "returnDetectedIssues", "valueBoolean": "true" },
                                                                            { "url" : "returnObservations", "valueBoolean": "true" },
                                                                            { "url" : "returnMedications", "valueBoolean": "true" },
                                                                            { "url" : "returnDosingGuidance", "valueBoolean": "true" },
                                                                            { "url" : "includePIMTable", "valueBoolean": "true" },
                                                                            { "url" : "includeDDIData", "valueBoolean": "false" },
                                                                            { "url" : "reportId", "valueString": "" }
                                                            ]
                                            }]
                            }              
            },
            {
                            "resource":{ 
                                            "resourceType": "ProcedureRequest", 
                                            "id": "17" 
                            }
            }]

}

Here is the code where I tried to do it and came close but having problems with nulls and one comma between the resources

        using System;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        var obj = new RootReq()
        {resourceType = "Bundle", type = "message", entry = new Entry[]{new Entry()
        {resource = new Resource()
        {resourceType = "MessageHeader", timestamp = DateTime.Now, id = "Test1", _event = new Event()
        {code = "diagnosticreport-provide"}, source = new Source()
        {endpoint = "http://yourdomain.com/api"}, destination = new Destination[]{new Destination()
        {endpoint = "https://api.pgxportal.com/api/$process-message"}}}}, new Entry()
        {resource = new Resource()
        {resourceType = "DiagnosticReport", extension = new Extension[]{new Extension()
        {url = "DiagnosticReportDefinition", extension = new Extension1[]{new Extension1()
        {url = "useNewMedications", valueBoolean = "false"}, new Extension1()
        {url = "providePDFReport", valueBoolean = "false"}, new Extension1()
        {url = "returnDetectedIssues", valueBoolean = "true"}, new Extension1()
        {url = "returnObservations", valueBoolean = "true"}, new Extension1()
        {url = "returnMedications", valueBoolean = "true"}, new Extension1()
        {url = "returnDosingGuidance", valueBoolean = "true"}, new Extension1()
        {url = "includePIMTable", valueBoolean = "true"}, new Extension1()
        {url = "includeDDIData", valueBoolean = "false"}, new Extension1()
        {url = "reportId", valueString = ""}, }}}}}, new Entry()
        {resource = new Resource()
        {resourceType = "ProcedureRequest", id = "17"}}}};
        var str = JsonConvert.SerializeObject(obj, Formatting.Indented);
        Console.WriteLine(str);
    }
}

public class RootReq
{
    public string resourceType
    {
        get;
        set;
    }

    public string type
    {
        get;
        set;
    }

    public Entry[] entry
    {
        get;
        set;
    }
}

public class Entry
{
    public Resource resource
    {
        get;
        set;
    }
}

public class Resource
{
    public string resourceType
    {
        get;
        set;
    }

    public DateTime timestamp
    {
        get;
        set;
    }

    public string id
    {
        get;
        set;
    }

    public Event _event
    {
        get;
        set;
    }

    public Source source
    {
        get;
        set;
    }

    public Destination[] destination
    {
        get;
        set;
    }

    public Extension[] extension
    {
        get;
        set;
    }
}

public class Event
{
    public string code
    {
        get;
        set;
    }
}

public class Source
{
    public string endpoint
    {
        get;
        set;
    }
}

public class Destination
{
    public string endpoint
    {
        get;
        set;
    }
}

public class Extension
{
    public string url
    {
        get;
        set;
    }

    public Extension1[] extension
    {
        get;
        set;
    }
}

public class Extension1
{
    public string url
    {
        get;
        set;
    }

    public string valueBoolean
    {
        get;
        set;
    }

    public string valueString
    {
        get;
        set;
    }
}

Here it what the wrong json that this code is producing looks like

      {
  "resourceType": "Bundle",
  "type": "message",
  "entry": [
    {
      "resource": {
        "resourceType": "MessageHeader",
        "timestamp": "2017-05-24T06:45:36.0632742+00:00",
        "id": "Test1",
        "_event": {
          "code": "diagnosticreport-provide"
        },
        "source": {
          "endpoint": "http://yourdomain.com/api"
        },
        "destination": [
          {
            "endpoint": "https://api.pgxportal.com/api/$process-message"
          }
        ],
        "extension": null
      }
    },
    {
      "resource": {
        "resourceType": "DiagnosticReport",
        "timestamp": "0001-01-01T00:00:00",
        "id": null,
        "_event": null,
        "source": null,
        "destination": null,
        "extension": [
          {
            "url": "DiagnosticReportDefinition",
            "extension": [
              {
                "url": "useNewMedications",
                "valueBoolean": "false",
                "valueString": null
              },
              {
                "url": "providePDFReport",
                "valueBoolean": "false",
                "valueString": null
              },
              {
                "url": "returnDetectedIssues",
                "valueBoolean": "true",
                "valueString": null
              },
              {
                "url": "returnObservations",
                "valueBoolean": "true",
                "valueString": null
              },
              {
                "url": "returnMedications",
                "valueBoolean": "true",
                "valueString": null
              },
              {
                "url": "returnDosingGuidance",
                "valueBoolean": "true",
                "valueString": null
              },
              {
                "url": "includePIMTable",
                "valueBoolean": "true",
                "valueString": null
              },
              {
                "url": "includeDDIData",
                "valueBoolean": "false",
                "valueString": null
              },
              {
                "url": "reportId",
                "valueBoolean": null,
                "valueString": ""
              }
            ]
          }
        ]
      }
    },
    {
      "resource": {
        "resourceType": "ProcedureRequest",
        "timestamp": "0001-01-01T00:00:00",
        "id": "17",
        "_event": null,
        "source": null,
        "destination": null,
        "extension": null
      }
    }
  ]
}
Scott Wein
  • 5
  • 1
  • 4
  • I am also having problem with c# not allowing the use of the instance of the Event object as named event not _event – Scott Wein May 24 '17 at 06:57
  • If you want to ignore nulls, then this might help: https://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-null-using-json-net – jason.kaisersmith May 24 '17 at 07:02
  • Thank you jason.kaisersmith that fixed most of the problems with this. Down to bad bracket closing and one bad name _event – Scott Wein May 24 '17 at 07:05
  • you can change the name the properties/fields serialize to or deserialize from with attributes: http://www.newtonsoft.com/json/help/html/SerializationAttributes.htm – Mats391 May 24 '17 at 07:08
  • Lastly the datetimes stamps still show up if you default(datetime) them as they cannot be null once they have been set a value – Scott Wein May 24 '17 at 07:17
  • 1
    `DateTime` is never `null`. If you want it to be `null` you will have to use the nullable type `Nullable` or `DateTime?` – Mats391 May 24 '17 at 07:20
  • I got this to work here is the code – Scott Wein May 24 '17 at 07:43

2 Answers2

0

Try this:

 var str= JsonConvert.SerializeObject(obj,
                            Newtonsoft.Json.Formatting.Indented,
                            new JsonSerializerSettings
                            {
                                NullValueHandling = NullValueHandling.Ignore
                            });
Elis
  • 91
  • 1
  • 4
0

Working now

        using System;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        var obj = new RootReq()
        {resourceType = "Bundle", type = "message", entry = new Entry[]{new Entry()
        {resource = new Resource()
        {resourceType = "MessageHeader", 
         timestamp = Convert.ToString(DateTime.Now),
         id = "Test1", 
         _event = new Event()
        {code = "diagnosticreport-provide"}, source = new Source()
        {endpoint = "http://yourdomain.com/api"}, destination = new Destination[]{new Destination()
        {endpoint = "https://api.com/api/$process-message"}}}}, new Entry()
        {resource = new Resource()
        {resourceType = "DiagnosticReport",
         timestamp = null,
         extension = new Extension[]{
            new Extension()

        {url = "DiagnosticReportDefinition", extension = new Extension1[]{new Extension1()
        {url = "useNewMedications", valueBoolean = "false"}, new Extension1()
        {url = "providePDFReport", valueBoolean = "false"}, new Extension1()
        {url = "returnDetectedIssues", valueBoolean = "true"}, new Extension1()
        {url = "returnObservations", valueBoolean = "true"}, new Extension1()
        {url = "returnMedications", valueBoolean = "true"}, new Extension1()
        {url = "returnDosingGuidance", valueBoolean = "true"}, new Extension1()
        {url = "includePIMTable", valueBoolean = "true"}, new Extension1()
        {url = "includeDDIData", valueBoolean = "false"}, new Extension1()
        {url = "reportId", valueString = ""}, }}}}}, new Entry()
        {resource = new Resource()
        {resourceType = "ProcedureRequest", id = "17"}}}};
        var str = JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings { 
                                NullValueHandling = NullValueHandling.Ignore
                            });
        Console.WriteLine(str);
    }
}

public class RootReq
{
    public string resourceType
    {
        get;
        set;
    }

    public string type
    {
        get;
        set;
    }

    public Entry[] entry
    {
        get;
        set;
    }
}

public class Entry
{
    public Resource resource
    {
        get;
        set;
    }
}

public class Resource
{
    public string resourceType
    {
        get;
        set;
    }

    public String timestamp
    {
        get;
        set;
    }

    public string id
    {
        get;
        set;
    }

    public Event _event
    {
        get;
        set;
    }

    public Source source
    {
        get;
        set;
    }

    public Destination[] destination
    {
        get;
        set;
    }

    public Extension[] extension
    {
        get;
        set;
    }
}

public class Event
{
    public string code
    {
        get;
        set;
    }
}

public class Source
{
    public string endpoint
    {
        get;
        set;
    }
}

public class Destination
{
    public string endpoint
    {
        get;
        set;
    }
}

public class Extension
{
    public string url
    {
        get;
        set;
    }

    public Extension1[] extension
    {
        get;
        set;
    }
}

public class Extension1
{
    public string url
    {
        get;
        set;
    }

    public string valueBoolean
    {
        get;
        set;
    }

    public string valueString
    {
        get;
        set;
    }
}
Scott Wein
  • 5
  • 1
  • 4