Home > Enterprise >  json arrays inside object read data conditionally with c# or java
json arrays inside object read data conditionally with c# or java

Time:10-25

I have this json:

    {
    "data":[
        {
            "id": "Character_Alien",
            "name": "Trespasser",
            "description": "Invader",
            "type": {
                "value": "outfit",
                "displayValue": "Outfit",
                "backendValue": "AthenaCharacter"
            },
            "rarity": {
                "value": "uncommon",
                "displayValue": "Uncommon"
            },
            "series": null,
            "set": null,
            "introduction": {
                "chapter": "2022",
                "text": "Introduced in 2022",
                "backendValue": 22
            },
            "images": {
                "smallIcon": "https://smallimage.png",
                "icon": "https://image.png",
                "featured": "https://imagebig.png",
                "other": null
            },
            "variants": null,
            "searchTags": null,
            "metaTags": null
        },
        {
            "id": "Character_Knowing",
            "name": "Sinister",
            "description": "He holds your fate.",
            "type": {
                "value": "outfit",
                "displayValue": "Outfit",
                "backendValue": "AthenaCharacter"
            },
            "rarity": {
                "value": "rare",
                "displayValue": "Rare",
                "backendValue": "EFortRarity::Rare"
            },
            "series": null,
            "set": {
                "value": "Malice",
                "text": "Path.",
                "backendValue": "GripHate"
            },
            "introduction": {
                "chapter": "2021",
                "backendValue": 22
            },
            "images": {
                "smallIcon": "https://smallimage.png",
                "icon": "https://image.png",
                "featured": "https://imagebig.png",
                "other": null
            },
            "variants": null,
            "searchTags": null,
            "metaTags": null
        }
    ]
}

It is a JSON 50000 lines long this is just 2 of the objects of the JSON. I would like to get the "name", the "images" and the "rarity" values only if the "displayValue" is "Outfit". There are a lot of different displayValues so I want to filter the Outfit value first and then I can figure out how to filter the other ones.

I would like to do it in C# but if it can be done easier using Java I can do it there too(I have basic knowledge on both of them)

I have this in mind:

Foreach object in the json
If the object.displayValue = outfit then
string name = object.name
string imagesmall = object.imagesmall
string image = object.image
string imagebig = object.imagebig
String rariry = object.rarity

If it can be done this way, I then want to generate an image for each outfit with the name and rarity in it as text.

Any links to a similar question would be appreciated, It is the 3rd day that I am looking how to do this and this is my last resort.

CodePudding user response:

You can be done this using Newtonsoft.Json NuGet easily. I have used the JSON as the string for the example. You can add your source for the relevant.

In case if you have a larger JSON with more attributes, you can use the Past Special option in Visual Studio to create the object Check Here

using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        string json = "{"  
            "\n\"data\":[\n"  
            "{\n\"id\":\"Character_Alien\",\n\"name\": \"Trespasser\",\n\"description\": "  
            "\"Invader\",\n\"type\": {\n\"value\": \"outfit\",\n\"displayValue\": \"Outfit\",\n\"backendValue\": "  
            "\"AthenaCharacter\"\n},\n\"rarity\": {\n\"value\": \"uncommon\",\n\"displayValue\": \"Uncommon\"\n}"  
            ",\n\"series\": null,\n\"set\": null,\n\"introduction\": {\n\"chapter\": \"2022\",\n\"text\": \"Introduced in 2022\","  
            "\n\"backendValue\": 22\n},"  
            "\n\"images\": {\n\"smallIcon\": \"https://smallimage.png\",\n\"icon\": \"https://image.png\","  
            "\n\"featured\": \"https://imagebig.png\",\n\"other\": null\n},\n\"variants\": null,\n\"searchTags\": null,"  
            "\n\"metaTags\": null\n},\n{\n\"id\": \"Character_Knowing\",\n\"name\": \"Sinister\","  
            "\n\"description\": \"He holds your fate.\",\n\"type\": {\n\"value\": \"outfit\",\n\"displayValue\": \"Outfit\","  
            "\n\"backendValue\": \"AthenaCharacter\"\n},"  
            "\n\"rarity\": {\n\"value\": \"rare\",\n\"displayValue\": \"Rare\",\n\"backendValue\": \"EFortRarity::Rare\"\n},"  
            "\n\"series\": null,\n\"set\": {\n\"value\": \"Malice\",\n\"text\": \"Path.\",\n\"backendValue\": \"GripHate\"\n},"  
            "\n\"introduction\": {\n\"chapter\": \"2021\",\n\"backendValue\": 22\n},\n\"images\": "  
            "{\n\"smallIcon\": \"https://smallimage.png\",\n\"icon\": \"https://image.png\",\n\"featured\": \"https://imagebig.png\","  
            "\n\"other\": null\n},\n\"variants\": null,\n\"searchTags\": null,\n\"metaTags\": null\n}\n]\n"  
        "}";


        var jsonArr = JsonConvert.DeserializeObject<JsonArr>(json);

        var val = jsonArr.Data.Where(w => w.Type.Value == "outfit").Select(s => new Data
        {
            Name = s.Name,
            Rarity = s.Rarity
        }).ToList();

        Console.WriteLine(json);
        Console.ReadKey();
    }
}


public class JsonArr
{
    [JsonProperty(PropertyName = "data")]
    public List<Data> Data { get; set; }
}
public class Data
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "description")]
    public string Description { get; set; }

    [JsonProperty(PropertyName = "type")]
    public DataType Type { get; set; }

    [JsonProperty(PropertyName = "rarity")]
    public Rarity Rarity { get; set; }


}
public class DataType
{
    [JsonProperty(PropertyName = "value")]
    public string Value { get; set; }

    [JsonProperty(PropertyName = "displayValue")]
    public string DisplayValue { get; set; }

    [JsonProperty(PropertyName = "backendValue")]
    public string BackendValue { get; set; }
    
}

public class Rarity
{
    [JsonProperty(PropertyName = "value")]
    public string Value { get; set; }

    [JsonProperty(PropertyName = "displayValue")]
    public string  DisplayValue { get; set; }

    [JsonProperty(PropertyName = "backendValue")]
    public string BackendValue { get; set; }
}

CodePudding user response:

.Net 6 (no external libraries needed):

using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Linq;
string json = "....."; // Removed for brevity
var results = ((JsonArray)(JsonNode.Parse(json))["data"])
    .Where(o => (string)(o["type"]["displayValue"]) == "Outfit")
    .Select(o => new 
            {
                Name = (string)o["name"],
                ImageSmall = (string)o["images"]["smallIcon"],
                Image = (string)o["images"]["icon"],
                ImageBig = (string)o["images"]["featured"],
                Rarity = (string)o["rarity"]["value"]
            });
foreach (var x in results) {
    Console.WriteLine(x);
}

Output:

{ Name = Trespasser, ImageSmall = https://smallimage.png, Image = https://image.png, ImageBig = https://imagebig.png, Rarity = uncommon }
{ Name = Sinister, ImageSmall = https://smallimage.png, Image = https://image.png, ImageBig = https://imagebig.png, Rarity = rare }
  • Related