0

I am trying to access all the values of OperationId in the below json and add it to an array, but it keep giving me a null exception as I drill down the data. Below is the json, any suggestion on how I can access operationIds directly are appreciated.

A Note: All the controller Names & HttpVerbs are variable throughout the json, this is just a small part from a 10,000 line Json file, so hardcoding each value to get a string doesn't work.

Json:

{
    "swagger": "2.0",
    "info": {
        "version": "v1",
        "title": "API Services"
    },
    "host": "TestApi.Com",
    "basePath": "/TestApi",
    "schemes": ["https"],
    "paths": {
        "/activity/actions": {
            "get": {
                "tags": ["activities"],
                "summary": "Returns a list of non-hidden Actions.",
                "description": "Test Description",
                ***"operationId": "GET/activity/actions",  //Need to add this to array***
                "consumes": [],
                "produces": ["application/json", "text/json", "application/xml", "text/xml"],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/ActionResponse"
                            }
                        }
                    }
                }
            },
            "put": {
                "tags": ["activities"],
                "summary": "Updates any/all existing ActivityActions by matching Id",
                "description": "Test Description",
                ***"operationId": "PUT/activity/actions", //Need to add this to array***
                "consumes": ["application/json", "text/json", "application/xml", "text/xml", "application/x-www-form-urlencoded"],
                "produces": ["application/json", "text/json", "application/xml", "text/xml"],
                "parameters": [{
                    "name": "List`1",
                    "in": "body",
                    "required": true,
                    "schema": {
                        "type": "array",
                        "items": {
                            "$ref": "#/definitions/ActivityActionPutRequest"
                        }
                    }
                }, {
                    "name": "Authorization",
                    "in": "header",
                    "description": "AccessToken",
                    "required": false,
                    "type": "string"
                }],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/ActivityActionUpdateResponse"
                            }
                        }
                    }
                }
            }
        },
        "/activity/agency-url/create": {
            "get": {
                "tags": ["activities"],
                "summary": "Test",
                ***"operationId": "GET/activity/agency-url/create",  //Need to add this to array***
                "consumes": [],
                "produces": ["application/json", "text/json", "application/xml", "text/xml"],
                "parameters": [{
                    "name": "EntityType",
                    "in": "query",
                    "description": "Test",
                    "required": true,
                    "type": "string"
                }, {
                    "name": "EntityId",
                    "in": "query",
                    "description": "The Guid or Sequence No of the entity.",
                    "required": true,
                    "type": "string"
                }, {
                    "name": "Authorization",
                    "in": "header",
                    "description": "AccessToken",
                    "required": false,
                    "type": "string"
                }],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/ActivityUrlResponse"
                        }
                    }
                }
            }
        }
    }
}

So Output will look like:

[GET/activity/actions, PUT/activity/actions, GET/activity/agency-url/create]

2 Answers 2

2

First define the JSON structure - this is only minimal structure that was needed to read the field you wanted:

public class RootJson
{
    public Paths paths { get; set; }
}

public class Paths : Dictionary<string, ActivityActions> { }

public class ActivityActions
{
    public Get get { get; set; }
    public Put put { get; set; }
}

public class Get
{
    public string operationId { get; set; }
}

public class Put
{
    public string operationId { get; set; }
}

Then extract the data like this (I'm reading all gets then all puts, you may want a different ordering):

var json = "{ ... define your json }";
var test = JsonConvert.DeserializeObject<RootJson>(json);

var result = new List<string>();
result.AddRange(test.paths.Select(p => p.Value.get?.operationId).Where(oid => oid != null));
result.AddRange(test.paths.Select(p => p.Value.put?.operationId).Where(oid => oid != null));

Now result holds the list you want.

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

Comments

1

So I would to use Newtonsoft's methods.

Something like this:

private static IEnumerable<JToken> GetToFindPropertiesAsJTokens(string json)
        {
            IEnumerable<JToken> jTokens = null;
            JObject jObject = JObject.Parse(json);
            jTokens = jObject.Descendants()
                .Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "operationId")
                .Select(p => ((JProperty)p).Value);
            return jTokens;
        }

Using:

 List<string> values = new List<string>();
 foreach (JToken jtoken in GetToFindPropertiesAsJTokensBasedOnTable(json))
 {
     values.Add(jtoken.ToString());
 }

 string[] valuesArray = values.ToArray();

 Array.ForEach(valuesArray, x=> Console.WriteLine(x));

Output:

GET/activity/actions
PUT/activity/actions
GET/activity/agency-url/create

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.