Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the following JSON. I want to fetch the rdfs:label's value when the type is @type": "owl:DbType and rdfs:class is given any class (GreenPlant/GreenPlantHistory)

For example: If @type": "owl:DbType and the class is GreenPlant, fetch rdfs:label's value that is 'Involved' & 'Present'

I have tried following query, but I am unable to add a class name in that. How do I do that?

Query:

JSON
List<string> jp = v1.Where(i => (string)i["@type"] == "owl:DbType")
  .Select(i => (string)((JObject)i).Properties().First(x => x.Name == "rdfs:label").Value["@value"]).ToList();

{
  "@id": "Ap:Involved",
  "@type": "owl:DbType",
  "rdfs:class": {
    "@id": "Ap:GreenPlant"
  },
  "tag:std:label": {
    "@value": "Involved"
  },
  "rdfs:label": {
    "@value": "Involved"
  },
  "rdfs:range": {
    "@id": "xsd:boolean"
  }
},

{
  "@id": "Ap:Present",
  "@type": "owl:DbType",
  "rdfs:class": {
    "@id": "Ap:GreenPlant"
  },
  "tag:std:label": {
    "@value": "Present"
  },
  "rdfs:label": {
    "@value": "Present"
  },
  "rdfs:range": {
    "@id": "xsd:boolean"
  }
},

{
  "@id": "Ap:UserName",
  "@type": "owl:DbType",
  "rdfs:class": {
    "@id": "Ap:GreenPlantHistory"
  },
  "tag:std:label": {
    "@value": "UserName"
  },
  "rdfs:label": {
    "@value": "UserName"
  },
  "rdfs:range": {
    "@id": "xsd:string"
  }
},

{
  "@id": "Ap:Name",
  "@type": "owl:ObjType",
  "rdfs:class": {
    "@id": "Ap:GreenPlantHistory"
  },
  "tag:std:label": {
    "@value": "Name"
  },
  "rdfs:label": {
    "@value": "Name"
  },
  "rdfs:range": {
    "@id": "xsd:string"
  }
}


What I have tried:

JSON
List<string> jp = v1.Where(i => (string)i["@type"] == "owl:DbType")
  .Select(i => (string)((JObject)i).Properties().First(x => x.Name == "rdfs:label").Value["@value"]).ToList();
Posted
Updated 14-Feb-22 8:54am
v2

1 solution

Start by defining classes that are compatible with the JSON data: Convert JSON to C# Classes Online - Json2CSharp Toolkit[^] will do that for you:
C#
public class RdfsClass
{
    [JsonProperty("@id")]
    public string Id { get; set; }
}

public class TagStdLabel
{
    [JsonProperty("@value")]
    public string Value { get; set; }
}

public class RdfsLabel
{
    [JsonProperty("@value")]
    public string Value { get; set; }
}

public class RdfsRange
{
    [JsonProperty("@id")]
    public string Id { get; set; }
}

public class Root
{
    [JsonProperty("@id")]
    public string Id { get; set; }

    [JsonProperty("@type")]
    public string Type { get; set; }

    [JsonProperty("rdfs:class")]
    public RdfsClass RdfsClass { get; set; }

    [JsonProperty("tag:std:label")]
    public TagStdLabel TagStdLabel { get; set; }

    [JsonProperty("rdfs:label")]
    public RdfsLabel RdfsLabel { get; set; }

    [JsonProperty("rdfs:range")]
    public RdfsRange RdfsRange { get; set; }
}
If you use those and a JSON reader package (I use Newtonsoft - you'll find it with the VS Package Manager) it's one line of code to deserialize the data to a set of classes. From that, it should be fairly easy to process your collections to get the data you want.

But we can't do that for you: there is only one element in the JSON you supply so there is no point in searching it ... feed your whole JSON into the converter and see what classes it creates.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900