1

How do I parse the following JSON to get the value of TestCycleName. The challenge is in identifying the root node since it starts with a number.

My implementation will be in C# using JSON.net

{
  "URL": "rest/zapi/latest/cycle?projectId=##projectId##&versionId=##versionId##",
  "Method": "GET",
  "Parameters": {
    "1": {
      "VersionName": "Custom Pipes Development",
      "TestCycleName": "SetMaxFutureDateFromCustomerField_Mobile"
    },
    "2": {
      "VersionName": "Recurring payments 1.5",
      "TestCycleName": "Internet Full Regression Pack - Mobile"
    },
    "3": {
      "VersionName": "Customer Profile Phase 1.5",
      "TestCycleName": "Customer Profile Regression Pack - Desktop"
    },
    "4": {
      "VersionName": "Customer Profile Phase 1.5",
      "TestCycleName": "Customer Profile E2E Pack - Desktop"
    },
    "5": {
      "VersionName": "Customer Profile Phase 1.5",
      "TestCycleName": "Customer Profile Regression Pack - Mobile"
    },
    "6": {
      "VersionName": "Internet Phase 1.2",
      "TestCycleName": "Internet API Regression Pack"
    },
    "7": {
      "VersionName": "Internet Phase 1.2",
      "TestCycleName": "Internet GUI Regression Pack - Desktop"
    },
    "8": {
      "VersionName": "Internet Phase 1.2",
      "TestCycleName": "Internet GUI Regression Pack - Mobile"
    },
    "9": {
      "VersionName": "Internet Phase 1.2",
      "TestCycleName": "Regression Library Admin Tool - E2E Tests"
    },
    "10": {
      "VersionName": "Internet Phase 1.2",
      "TestCycleName": "Regression Library E2E Tests - Mobile"
    },
    "11": {
      "VersionName": "Recurring payments 1.5",
      "TestCycleName": "[Internet] Autopay API Automation Regression Pack"
    }
  }
}
SᴇM
  • 7,024
  • 3
  • 24
  • 41
mach
  • 111
  • 1
  • 2
  • 11
  • 2
    Its a dictionary, so in your classes, just use a Dictionary – TheGeneral May 24 '18 at 07:11
  • Copy this json string, then go to Visual studio,_**Edit -> Paste Special -> Paste JSON as Classes**_ – SᴇM May 24 '18 at 07:11
  • Possible duplicate: https://stackoverflow.com/questions/23552708/how-to-parse-json-with-number-as-a-key – Abdullah Dibas May 24 '18 at 07:12
  • If you can, get the people that made the JSON to change it so it returns an array rather than a list of separate objects. – Hans Kilian May 24 '18 at 07:25
  • Use a `Dictionary` for `Parameters` as recommended in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182) – dbc May 24 '18 at 07:52

3 Answers3

3

Parameters is a dictionary, so you can write something like this:

public class Rootobject
{
    public string URL { get; set; }
    public string Method { get; set; }
    public Dictionary<int, Parameter> Parameters { get; set; }
}

public class Parameter
{
    public string VersionName { get; set; }
    public string TestCycleName { get; set; }
}

and then deserialize it:

var result = JsonConvert.DeserializeObject<Rootobject>(json_string);

//You can get your TestCycleNames for example in string array
string[] testCycleName = result.Parameters.Select(p => p.Value.TestCycleName).ToArray(); 
SᴇM
  • 7,024
  • 3
  • 24
  • 41
0

Try something like this (not tested of-course, but you can debug it):

// You get Parameters object
var parameters = JObject.Parse(jsonString)["Parameters"];

// parameters can further be accessed via key/value pair using JProperty
foreach( var item in parameters.OfType<JProperty>()){

  // each item has a key and a value i.e item.Name and item.Value, 
  // key = numeric in your data, value is a json object.
  // take object and further parse it to reach TestCycleName

  var innerObject = JObject.Parse(item.Value.ToString())["TestCycleName"];

  Console.WriteLine(innerObject.ToString());

}

I had something similar, check it out here: Iterating JObject Keys

Waleed
  • 1,097
  • 18
  • 45
  • getting this error cannot convert from 'Newtonsoft.Json.Linq.JToke' to string at item.value – mach May 24 '18 at 08:15
  • @mach As I said, I wrote it here in Stackoverflow, didn't test it. Updated my answer. Need to convert Value to string because JObject.Parse takes string to parse. – Waleed May 24 '18 at 09:26
-1

Try codes as follows:

using Newtonsoft.Json.Linq;
using System;
using System.Linq;


namespace ConsoleApp1
{
  class Program
  {
    static void Main(string[] args)
    {
        string jsonString = "{\"URL\": \"rest/zapi/latest/cycle?projectId=##projectId##&versionId=##versionId##\",\"Method\": \"GET\",\"Parameters\": {\"1\": {\"VersionName\": \"Custom Pipes Development\",\"TestCycleName\": \"SetMaxFutureDateFromCustomerField_Mobile\"},\2"\": {\"VersionName\": \"Recurring payments 1.5\",\"TestCycleName\": \"Internet Full Regression Pack - Mobile\"}}}";

        var parameters = JObject.Parse(jsonString)["Parameters"];

        foreach (var item in parameters.OfType<JProperty>())
        {
            var innerObject = JObject.Parse(item.Value.ToString())["TestCycleName"];

            Console.WriteLine(innerObject.ToString());
        }

        Console.ReadLine(); 
    }
  }
}
willzh
  • 69
  • 6