1

I am recieving this response from a GraphQL endpoint:

"container": {
                            "id": "X",
                            "containerId": "XYZ",
                            "metaData":"{\"postnummer\": \"9000 Aalborg\", \"ejendomsnr\": \"12345\", \"kundenr\": \"12345\", \"address\": \"\Ågade 5 C, 9000 Aalborg\", \"vej\": \"\Ågade\", \"nr\": \"5 C\"}",
                            "wasteFraction": {
                                "wasteCategory": "Combustion",
                                "wasteSubstance": "Gaseous",
                                "wasteTypes": {
                                    "edges": [
                                        {
                                            "node": {
                                                "name": "Residual waste"
                                            }
                                        }
                                    ]
                                }
                            }
                        }

My problem is with the metaData attribute, which itself is a json string, which the GraphQLHttpClient/NewtonsoftJsonSerializer does not seem to handle.

If I define the property as an object with the correct properties, serialization fails. If I define metaData like a string, I then have to manually deserialize it afterwards.

This works, but then I have to manually desialize the string for each result:

public class Container
{
    public string id { get; set; }
    public string containerId { get; set; }
    public string metaData { get; set; }
    public Wastefraction wasteFraction { get; set; }
}

public class MetaData
{
    public string postnummer { get; set; }

    public string husnr { get; set; }
    public string postdistrikt { get; set; }

    public string ejendomsnr { get; set; }
    public string kundenr { get; set; }
    public string address { get; set; }
    public string vej { get; set; }
    public string nr { get; set; }
}

This does NOT work:

public class Container
{
    public string id { get; set; }
    public string containerId { get; set; }
    public MetaData metaData { get; set; }
    public Wastefraction wasteFraction { get; set; }
}

public class MetaData
{
    public string postnummer { get; set; }

    public string husnr { get; set; }
    public string postdistrikt { get; set; }

    public string ejendomsnr { get; set; }
    public string kundenr { get; set; }
    public string address { get; set; }
    public string vej { get; set; }
    public string nr { get; set; }
}

Is it possible to get the GraphQLHttpClient/NewtonsoftJsonSerializer to handle this nested jsonstring automaticly?

Kennedine
  • 23
  • 5
  • You should probably fix whatever is generating this, so that it doesn't double-serialize in the first place. – Charlieface Oct 12 '21 at 11:25
  • I would love to do that, but I do not control the Endpoint and the field is a custom field each customer can define on their own. So they tell it cannot be any other way.. Someone suggested a solution from a different thread which worked perfectly. – Kennedine Oct 13 '21 at 08:55

0 Answers0