0

I have a json file where I want to search for a device (ex. AEVL2020) and will return multiple results based on "registers". I tried to search on the net but didn't find any clue. So I want an output like this:

 "12":{
                "user_id": "1",
                "employee_id": "12",
                "name": "Juan Dela Cruz",
                "privilege": "0"
            },
            "32":{
                "user_id": "2",
                "employee_id": "32",
                "name": "Pedro Dela Cruz",
                "privilege": "0"
            }

Here is my json:

{
        "devices": {
            "AEVL2020":{
                "deviceSerialId": "AEVL2020"
            },
            "AEVL2021":{
                "deviceSerialId": "AEVL2021"
            }
        },
        "registers":{
            "AEVL2020":{
                "12":{
                    "user_id": "1",
                    "employee_id": "12",
                    "name": "Juan Dela Cruz",
                    "privilege": "0"
                },
                "32":{
                    "user_id": "2",
                    "employee_id": "32",
                    "name": "Pedro Dela Cruz",
                    "privilege": "0"
                }
            },
            "AEVL2021":{
                "29":{
                    "user_id": "1",
                    "employee_id": "29",
                    "name": "Maria Dela Cruz",
                    "privilege": "0"
                },
                "222":{
                    "user_id": "2",
                    "employee_id": "222",
                    "name": "Jay Dela Cruz",
                    "privilege": "0"
                }
            }
        }
    }

2 Answers 2

3

using Newtonsoft.Json

var json = JObject.Parse("{\"devices\":{\"AEVL2020\":{\"deviceSerialId\":\"AEVL2020\"},\"AEVL2021\":{\"deviceSerialId\":\"AEVL2021\"}},\"registers\":{\"AEVL2020\":{\"12\":{\"user_id\":\"1\",\"employee_id\":\"12\",\"name\":\"Juan Dela Cruz\",\"privilege\":\"0\"},\"32\":{\"user_id\":\"2\",\"employee_id\":\"32\",\"name\":\"Pedro Dela Cruz\",\"privilege\":\"0\"}},\"AEVL2021\":{\"29\":{\"user_id\":\"1\",\"employee_id\":\"29\",\"name\":\"Maria Dela Cruz\",\"privilege\":\"0\"},\"222\":{\"user_id\":\"2\",\"employee_id\":\"222\",\"name\":\"Jay Dela Cruz\",\"privilege\":\"0\"}}}}");

var devices = json.Value<JObject>("devices");
var registers = json.Value<JObject>("registers");

foreach (var device in devices)
{
    Console.WriteLine($"Device {device.Key}:");
    Console.WriteLine(registers[device.Value.Value<string>("deviceSerialId")]);
}

I have output like you want

Device AEVL2020:
{
  "12": {
    "user_id": "1",
    "employee_id": "12",
    "name": "Juan Dela Cruz",
    "privilege": "0"
  },
  "32": {
    "user_id": "2",
    "employee_id": "32",
    "name": "Pedro Dela Cruz",
    "privilege": "0"
  }
}

Device AEVL2021:
{
  "29": {
    "user_id": "1",
    "employee_id": "29",
    "name": "Maria Dela Cruz",
    "privilege": "0"
  },
  "222": {
    "user_id": "2",
    "employee_id": "222",
    "name": "Jay Dela Cruz",
    "privilege": "0"
  }
}

p.s.: probably you would read json from file, so replace the first line to

var json = JObject.Parse(File.ReadAllText("file.json"));
Sign up to request clarification or add additional context in comments.

Comments

0

Another similar way:

var obj = JsonConvert.DeserializeObject(json) as JObject;
var reg = obj["registers"]["AEVL2020"];

Or using a loop:

foreach (var deviceName in obj["devices"].Children<JProperty>().Select(x => x.Name))
{
    var reg = obj["registers"][deviceName];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.