inquisitive_one
inquisitive_one

Reputation: 1495

LINQ on polymorphic json to get a comma separated field

I have a polymorphic json string. Here's what it looks like:

{
    "?xml" : {
        "@version" : "1.0",
        "@encoding" : "UTF-8"
    },
    "DataFeed" : {
        "@FeedName" : "complianceCompany",
        "Companies" : {
            "@timeStamp" : "2016-07-06T10:16:51.00",
            "Company" : [{
                    "@id" : "1",
                    "Ticker" : "MSFT-NSDQ",
                    "Name" : "Microsoft",
                    "Disclosure" : {
                        "@Code" : "25",
                        "@Description" : "25: PERSON FINANCIAL INTEREST"
                    }
                }, {
                    "@id" : "3",
                    "Ticker" : "AAPL-NSDQ",
                    "Name" : "Apple",
                    "Disclosure" : [{
                            "@Code" : "3",
                            "@Description" : "03: RECEIVED COMP FOR NON-IB SVS"
                        }, {
                            "@Code" : "6C",
                            "@Description" : "06C: CLIENT NON SEC REL SVS"
                        }, {
                            "@Code" : "39",
                            "@Description" : "39: MARKET MAKER"
                        }
                    ]
                }
            ]
        }
    }
}

Someone once helped me with the following extension class:

public static class JsonExtensions
{
    public static IEnumerable<JObject> ObjectsOrSelf(this JToken root)
    {
        if (root is JObject)
            yield return (JObject)root;
        else if (root is JContainer)
            foreach (var item in ((JContainer)root).Children())
                foreach (var child in item.ObjectsOrSelf())
                    yield return child;
        else
            yield break;
    }
}

Based on that, here's my query:

JObject feed = JObject.Parse(jsonText);

var discInfo = from issuer in feed.SelectTokens("DataFeed.Companies.Company").SelectMany(i => i.ObjectsOrSelf())
   let discs = issuer.SelectTokens("Disclosure").SelectMany(s => s.ObjectsOrSelf())
   select new
    {
        Id = (int)issuer["@id"],
        Ticker = (string)issuer["Ticker"],
        CompName = (string)issuer["Name"],
        DiscCode = (string)discs["@Code"],
    };

When I tried the query, I get the following error:

Cannot apply indexing with [] to an expression of type 'IEnumerable<JObject>'

I'm trying to get all the discs in a comma separated field for each ticker. The end result should look like this:

End result

How can I get the end result?

Upvotes: 0

Views: 222

Answers (1)

Good Night Nerd Pride
Good Night Nerd Pride

Reputation: 8462

When you do...

DiscCode = (string)discs["@Code"]

...you are trying to access the property "@Code" of a disclosure JObject. But discs is of type IEnumerable<JObject>! You have to collect all "@Code"s in discs and concatenate them.

LINQ's Aggregate() can do this, but I think it always looks more readable to use string.Join() for that:

DiscCode = string.Join(", ", discs.Select(d => (string)d["@Code"]))

Upvotes: 1

Related Questions