0

I'm trying to construct a request body for a REST api call, and I need to create a JSON object with the list of properties I want to get back. For eg: I have this C# object that I want to get back:

public class SomeProperties
{
    public string TicketNumber { get; set; }
    public Driver Driver { get; set; }
}

public class Driver
{
    public string Name { get; set; }
}

To get this back, I need to put these properties in a JSON request body like this:

"properties": [
"ticketNumber",
"driver.name"
]

My attempt looks like this:

private string FetchProperties()
{
    var fetchProperties = new
    {
        properties = new List<string>
                        {
                            "ticketNumber",
                            "driver.name"
                        }
    };

    var jsonResult = JsonConvert.SerializeObject(fetchProperties, Formatting.None);
    return jsonResult;
}

But I don't want to hard code the properties like that.

So is there any way I can use property names from the object I want, to put in the list of strings that I made in the method above?

Thank You!

3
  • I don't exactly understand your problem. You are trying to serialize an expected list of properties for an object graph by mapping the Type to a list of camel-cased jsonpath property names. But you want to deserialize actual instance property values on the receiving end? How can you map type information to instance data? Commented Jun 4, 2020 at 16:32
  • Hi @dbc, I just updated the question. Thank you. Commented Jun 9, 2020 at 21:38
  • Sounds like you might want to use a graph query. While possible using REST it is quite a bit of work. There are some answers here which may help: stackoverflow.com/questions/40871190/… Commented Jun 9, 2020 at 22:02

2 Answers 2

0

If I understand correctly,you need Metadata of model. if you use EntityFramework, you can get metadata of your model

from this Code

and call BuildJsonMetadata() function

and if you use other mapper, I dont see any exist tool for generate metadata of model and you must generate it handly somthing like this

Sign up to request clarification or add additional context in comments.

Comments

0

First of, if you serialize the class you have (SomeProperties), you will not get driver.name. Instead you will get a string like this one that shows driver as an object,

{
  properties : {
    "ticketNumber" : "stringvalue",
    "driver"       : {
      "name" : "stringValue"
    }
  }
}

That said, if you are interested in getting a json like this,

"properties": [
  "ticketNumber",
  "driver.name"
]

you will need a class (very simple one at that) that contains only a list of strings. properties is not an array of objects, but simply strings. From the looks of the FetchProperties method, you are creating an object with fetchProperties as the RootObject. Try something like this,

public class MyClass
{
    [JsonProperty("fetchProperties")]
    public Fetch FetchProperties { get; set; }
}
public class Fetch
{
    [JsonProperty("properties")]
    public List<string> Properties { get; set; }
}

private string FetchProperties()
{
    MyClass obj = new MyClass()
    {
        FetchProperties = new Fetch()
        {
            Properties = new List<string>() { "ticketNumber", "driver.Name" }
        }
    };
    return JsonConvert.SerializeObject(obj); // Formatting.None is by default
}

Now its your choice to hard code these values or, pass them as arguments or use a local variable that contains a list of all the strings you intend to store as "properties". You cant use enums because of violation in naming convention (driver.name) so these options should suffice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.