0

I am trying to create an array from a .json array and get only 2 variables in the array (keyName and token) out of the whole current .json array.

The .json that is being received by an api call:

{
  "project": [
   {
      "id": 1,
      "keyName": "John123",
      "age": "19",
      "token": "123456789"
    },
    {
      "id": 2,
      "keyName": "Mary123",
      "age": "13",
      "token": "23435"
    },
    {
      "id": 3,
      "keyName": "Harry123",
      "age": "23",
      "token": "2343542"
    }
  ] 
}

I only want an array that has the keyName and token being shown for each object, how can I do this?

At the moment I have used an api call to get the .json file then I have parsed it into an array as shown below:

 Projects = JArray.Parse(resp.Content);

Please help as I am new to C#, sorry if some doesn't make sense my english isnt very good.

blakeh
  • 73
  • 2
  • 7
  • What you show is NOT valid JSON. Please show what you actually receive, without mangling it up. – Peter B Feb 28 '21 at 23:31

3 Answers3

0

You can deserialize JSON file with help of JsonSerializer.

First thing you need to do is create C# class that will describe json data. It can be regular POCO class. In your case it will look similar to this:

public class Foo
{
    public string KeyName {get; set;}
    public string Token {get; set;}
}

Then all you need to do is get data from API via HttpClient and call .Deserialize method of JsonSerializer.

Neistow
  • 806
  • 1
  • 8
  • 20
0

I would suggest starting with the format of the json document, to be able to deserialize the object.

Valid json format:

{
  "project": [
   {
      "id": 1,
      "keyName": "John123",
      "age": "19",
      "token": "123456789"
    },
    {
      "id": 2,
      "keyName": "Mary123",
      "age": "13",
      "token": "23435"
    },
    {
      "id": 3,
      "keyName": "Harry123",
      "age": "23",
      "token": "2343542"
    }
  ] 
}

You should then create a Key object:

public class Project
{       
    public int id { get; set; }
    public string keyname { get; set; }
    public string age {get;set;}
    public string token {get;set;}
}

You can now serialize the json as an array of the key object, and query it via Linq

    List<Project> projects = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Project>>("content of the json file");
    var keyAndToken = projects.Select(c=> c.keyname, c.token);
0
{
  "project": [
   {
      "id": 1,
      "keyName": "John123",
      "age": "19",
      "token": "123456789"
    },
    {
      "id": 2,
      "keyName": "Mary123",
      "age": "13",
      "token": "23435"
    },
    {
      "id": 3,
      "keyName": "Harry123",
      "age": "23",
      "token": "2343542"
    }
  ] 
}

You can use Newtonsoft.Json framework for json manipulation, which you can install from NuGet Package Manager. Then do a Project model with the data that you need. You can abstract it even further with JSONData class, so if your json data grows you can just add new properties to it. After that just deserialize it.

public class Project
{
    public string KeyName { get; set; }
    public string Token { get; set; }
}

public class JSONData
{
    public List<Project> Projects { get; set; }
}

public class Deserializer
{
    //---------------- FIELDS -----------------
    private readonly string path = @"../../yourJsonFile.json";
    private readonly JSONData jsonData;
    
    //------------- CONSTRUCTORS --------------
    public Deserializer() {
        this.jsonData = JsonConvert.DeserializeObject<JSONData>(File.ReadAllText(this.path));
    }
}

That way you won't duplicate deserialization code like this

JsonConvert.DeserializeObject<List<Project>>(path);
JsonConvert.DeserializeObject<List<Category>>(path);
JsonConvert.DeserializeObject<List<Product>>(path);
radrex
  • 179
  • 1
  • 7