Resolved Preserving Legacy Data

ESoltay

Member
Joined
Feb 1, 2022
Messages
10
Programming Experience
10+
I have been importing a JSON file with VS script for about 6 yrs. Variables have been added during the past 6yrs and I was able to handle the legacy data with the added code 'ContainsKey' on the mapping code. (ex.
C#:
nba_draw_result = record.ContainsKey("nba_draw_result") == true && record["nba_draw_result"] != "" ? record["nba_draw_result"] : null
)

Recently a variable was added ("grouping_uuid") within the 'metadata' array and I'm having trouble trying to utilize the ContainsKey with a list name/ChildrenToken within an array. (ex
C#:
grouping_uuid = record.ContainsKey(record["metadata"]["grouping_uuid"]) == true && record["metadata"]["grouping_uuid"] != "" ? Convert.ToInt32(record["metadata"]["grouping_uuid"]) : null
).

simplified version of the JSON file:
C#:
[
  {
    "nba_draw_result": "",
    "metadata": {
      "grouping_uuid": 193504398
    }
  },
]
I've been struggling for about a week and any help will be greatly appreciated. Thank you.

Best Regards
 
Last edited by a moderator:
What JSON library are you using? With the 6 year longevity of your code, I would guess Newtonsoft, but I don't want to make that assumption.
 
C#:
using System;
using Newtonsoft.Json.Linq;

namespace SimpleCS
{
    static class StringExtensions
    {
        public static string EmptyToNull(this string s)
            => String.IsNullOrEmpty(s) ? null : s;
    }

    class Program
    {
        static void Main(string[] args)
        {
            var record = JObject.Parse(
                @"{
                    'nba_draw_result': '',
                    'metadata': {
                                'grouping_uuid': 193504398
                    }
                }");

            var nba_draw_result = record.SelectToken("nba_draw_result")
                                        ?.Value<string>()
                                        .EmptyToNull();
            var grouping_uuid = record.SelectToken("metadata.grouping_uuid")
                                      ?.Value<int>();
        }
    }
}

The SelectToken() method lets you do an XPath style query using something called JPath. If it finds something it'll return a JToken, otherwise it'll return null.
The ?. null conditional operator (aka elvis operator) lets you continue on to return a null, otherwise it accesses the object to the left.
The Value<T> performs the necessary cast/conversion of the JToken to the type T.

I had to roll my own EmptyToNull() extension method, because you seem to have a convention of treating an empty string as a null.
 
Sorry, I should have clarified in more depth. Dictionary does not contain 'SelectToken'
C#:
Dictionary<dynamic, dynamic>[] FWPJsonObj = JsonConvert.DeserializeObject<Dictionary<dynamic, dynamic>[]>(File.ReadAllText(JSONFile));

            foreach (var record in FWPJsonObj)
            {


                using (var context = new NCBDDD_TTCEntities())
                {
                    try
                    {
                        var udc2_fwp_json = new UDC2_FWP_JSON
                        {

nba_draw_result = record.ContainsKey("nba_draw_result") == true && record["nba_draw_result"] != "" ? record["nba_draw_result"] : null,
grouping_uuid = record.ContainsKey(record["metadata"]["grouping_uuid"]) == true && record["metadata"]["grouping_uuid"] != "" ? Convert.ToInt32(record["metadata"]["grouping_uuid"]) : null
}
}
}
}
 
Last edited by a moderator:
I guess you have your reasons for parsing into a dictionary even though JSON.NET's Parse() already gives you that effect.

Anyway, you'll have so choice at this point. First check to see if there is a key for "metadata". If it present, retrieve that object. The check if that object contains a key for "grouping_uuid". If it does, then retrieve that object, and check the value. If the value is not an empty string, parse the string into an integer.
 
My reasoning for the path that I took 6 yrs ago was that I knew nothing about importing JSON data and there was a class in Pluralsight about this very thing. :) I've set a breakpoint and looked at breakout and 'grouping_uuid' is under the ChildrenToken. The only key is metadata. I need to somehow check the existence of the ChildrenToken within the metadata array. I know you're not found of screenshots . . . .

1643747882479.png





Surely this cannot be the first ever instance where the knowledge of an existing item within an array has been inquired? Have I stumbled onto something here?


Signed Pulling My Hair
 
Anyway, I hope this helps:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace SimpleCS
{
    static class StringExtensions
    {
        public static string EmptyToNull(this string s)
            => String.IsNullOrEmpty(s) ? null : s;
    }

    class Program
    {
        const string Json = @"[ {
                    'nba_draw_result': '',
                    'metadata': {
                                'grouping_uuid': 193504398
                    }
                } ]";

        static void UseDynamicDictionaries()
        {
            var records = JsonConvert.DeserializeObject<Dictionary<dynamic, dynamic>[]>(Json);

            var record = records.First();

            var nba_draw_result = record.ContainsKey("nba_draw_result") &&
                                  record["nba_draw_result"] != ""
                                    ? record["nba_draw_result"]
                                    : null;
            var grouping_uuid = record.ContainsKey("metadata") &&
                                record["metadata"].ContainsKey("grouping_uuid")
                                    ? record["metadata"]["grouping_uuid"]
                                    : null;
        }

        static void UseJObjects()
        {
            var records = JArray.Parse(Json);

            var record = records.First();

            var nba_draw_result = record.SelectToken("nba_draw_result")
                                        ?.Value<string>()
                                        .EmptyToNull();
            var grouping_uuid = record.SelectToken("metadata.grouping_uuid")
                                      ?.Value<int>();    
        }

        static void Main(string[] args)
        {
            UseJObjects();
            UseDynamicDictionaries();
        }
    }
}
 
Last edited:
ERROR MESSAGE
RuntimeBinderException
from "20220101_20220130_registry_subsequent_form.json", record (study_subject_identifier) "35M8JUAWG " mapping to the Parent table "UDC2_FWP_JSON". Message = "'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'ContainsKey'".


I believe it threw the error on the second use of ContainsKey:

var grouping_uuid = record.ContainsKey("metadata") && record["metadata"].ContainsKey("grouping_uuid") ? record["metadata"]["grouping_uuid"] : null;
 
Post the raw JSON data in code tags please so that we all can try debugging.

Right now, In have my doubts because C# does short circuit processing of booleans going from left to right. record.ContainsKey("metadata") needs to be true first, before it tried to process record["metadata"].ContainsKey("grouping_uuid").
 
See my code in post #8 with the simplified JSON that you first offered. It works without issue. If you could post something similar which reproduces the problem.
 
C#:
[
    {
        "date_of_visit_year": "2017",
        "date_of_visit_month": "6",
        "date_of_visit_day": "6",
        "date_of_last_surveillance_visit_year": "2017",
        "date_of_last_surveillance_visit_month": "4",
        "date_of_last_surveillance_visit_day": "4",
        "weight": "",
        "weight_unknown": "Unknown",
        "date_of_weight_collected_year": "",
        "date_of_weight_collected_month": "",
        "date_of_weight_collected_day": "",
        "height": "",
        "height_unknown": "Unknown",
        "date_of_height_collected_year": "",
        "date_of_height_collected_month": "",
        "date_of_height_collected_day": "",
        "zip_code": "85950",
        "health_insurances": [
            "Indian Health Service"
        ],
        "health_insurances_segments": {
            "Indian Health Service": false
        },
        "patient_highest_education_level": "Pre-elementary",
        "patient_grade_level": null,
        "employment_status": "Employed full-time",
        "family_history_of_bleeding_disorder": "false",
        "genetic_mutation": "false",
        "htc_service_locations": [
            "HTC outreach clinic"
        ],
        "used_hemlibra_prior_to_collection": "false",
        "cba_draw_not_available": "",
        "cba_draw_date_estimation_type": "",
        "cba_draw_date_year": "",
        "cba_draw_date_month": "",
        "cba_draw_date_day": "",
        "cba_draw_result": "",
        "cba_draw_result_other": "",
        "cba_draw_result_is_negative": "",
        "nba_draw_not_available": "",
        "nba_draw_date_estimation_type": "",
        "nba_draw_date_year": "",
        "nba_draw_date_month": "",
        "nba_draw_date_day": "",
        "nba_draw_result": "",
        "nba_draw_result_other": "",
        "nba_draw_result_is_negative": "",
        "inhibitor_not_done": "No Results Available",
        "inhibitor_titer_draw_date_estimation_type": "",
        "inhibitor_titer_draw_date_year": "",
        "inhibitor_titer_draw_date_month": "",
        "inhibitor_titer_draw_date_day": "",
        "inhibitor_result": "",
        "inhibitor_result_not_detected": "",
        "below_inhibitor_reference_value": "",
        "highest_historic_inhibitor_titer_value": "",
        "highest_historic_inhibitor_titer_unknown": "Unknown",
        "has_history_of_iti": "false",
        "highest_prior_inhibitor_titer_value": "",
        "highest_prior_inhibitor_titer_unknown": "",
        "iti_use_since_last_reporting_period": [],
        "history_of_immune_modulation_therapy": "false",
        "current_products": null,
        "current_products_unknown": "Unknown",
        "current_treatment_regimen": "Episodic (on-demand)",
        "use_of_clotting_factor_concentrate": "",
        "prophylaxis_start_date_known": "",
        "prophylaxis_start_date_estimation_type": "",
        "prophylaxis_start_date_year": "",
        "prophylaxis_start_date_month": "",
        "prophylaxis_start_date_day": "",
        "prophylaxis_age_estimation_type": "",
        "prophylaxis_age_unit": "",
        "prophylaxis_age_value": "",
        "joint_bleeds_before_prophylaxis": "",
        "approximate_percentage_of_missed_doses": "",
        "active_prophylaxis_medications": [],
        "use_of_clotting_factor_concentrate_unknown": null,
        "products_used_since_last_udc_visit": null,
        "products_used_unknown": "None",
        "history_of_allergic_reaction": "false",
        "historical_allergens": null,
        "diagnosed_with_tm_since_last_visit": "false",
        "history_of_cvad_placement": "false",
        "history_of_cvad_complications": "false",
        "cvad_complications": null,
        "cvad_complications_unknown": "",
        "history_of_ich": "false",
        "ich_events": [],
        "history_of_joint_bleeds": "false",
        "is_first_joint_bleed_since_last_visit": "",
        "first_joint_bleed_start_date_known": "",
        "first_joint_bleed_age_estimation_type": "",
        "first_joint_bleed_age_value": "",
        "first_joint_bleed_age_unit": "",
        "first_joint_bleed_estimation_type": "",
        "first_joint_bleed_year": "",
        "first_joint_bleed_month": "",
        "first_joint_bleed_day": "",
        "number_of_bleeds_ankle_left": "",
        "number_of_bleeds_ankle_right": "",
        "number_of_bleeds_elbow_left": "",
        "number_of_bleeds_elbow_right": "",
        "number_of_bleeds_hip_left": "",
        "number_of_bleeds_hip_right": "",
        "number_of_bleeds_knee_left": "",
        "number_of_bleeds_knee_right": "",
        "number_of_bleeds_shoulder_left": "",
        "number_of_bleeds_shoulder_right": "",
        "collection_method_ankle_left": null,
        "collection_method_ankle_right": null,
        "collection_method_elbow_left": null,
        "collection_method_elbow_right": null,
        "collection_method_hip_left": null,
        "collection_method_hip_right": null,
        "collection_method_knee_left": null,
        "collection_method_knee_right": null,
        "collection_method_shoulder_left": null,
        "collection_method_shoulder_right": null,
        "number_of_bleeds_ankle_left_unknown": "",
        "number_of_bleeds_ankle_right_unknown": "",
        "number_of_bleeds_elbow_left_unknown": "",
        "number_of_bleeds_elbow_right_unknown": "",
        "number_of_bleeds_hip_left_unknown": "",
        "number_of_bleeds_hip_right_unknown": "",
        "number_of_bleeds_knee_left_unknown": "",
        "number_of_bleeds_knee_right_unknown": "",
        "number_of_bleeds_shoulder_left_unknown": "",
        "number_of_bleeds_shoulder_right_unknown": "",
        "treated_bleed_events_collection_methods": [
            "Paper Log"
        ],
        "treated_bleed_events_count": "4",
        "history_of_general_bleeds": "No",
        "history_of_gastrointestinal_bleeds": "",
        "history_of_genitourinary_hematuria_bleeds": "",
        "history_of_genitourinary_other_bleeds": "",
        "history_of_head_extracranial_bleeds": "",
        "history_of_muscle_bleeds": "",
        "history_of_nasal_mucosa_bleeds": "",
        "history_of_oral_bleeds": "",
        "history_of_soft_tissue_bleeds": "",
        "history_of_spine_bleeds": "",
        "history_of_other_bleeds": "",
        "associated_with_gastrointestinal_bleeds": null,
        "associated_with_genitourinary_hematuria_bleeds": null,
        "associated_with_genitourinary_other_bleeds": null,
        "associated_with_head_extracranial_bleeds": null,
        "associated_with_muscle_bleeds": null,
        "associated_with_nasal_mucosa_bleeds": null,
        "associated_with_oral_bleeds": null,
        "associated_with_soft_tissue_bleeds": null,
        "associated_with_spine_bleeds": null,
        "associated_with_other_bleeds": null,
        "history_of_invasive_joint_procedure": "No",
        "history_of_synovectomy_arthroscopic": "",
        "history_of_synovectomy_open": "",
        "history_of_synovectomy_radioisotopic": "",
        "history_of_synovectomy_unknown": "",
        "history_of_surgical_fusion_arthrodesis": "",
        "history_of_joint_replacement_arthroplasty": "",
        "history_of_other_joint_procedure": "",
        "joint_locations_synovectomy_arthroscopic": null,
        "joint_locations_synovectomy_open": null,
        "joint_locations_synovectomy_radioisotopic": null,
        "joint_locations_synovectomy_unknown": null,
        "joint_locations_surgical_fusion_arthrodesis": null,
        "joint_locations_joint_replacement_arthroplasty": null,
        "joint_locations_other_joint_procedure": null,
        "effect_on_activity_level": "Limited school or work and limited recreational activity levels",
        "has_chronic_pain": "false",
        "chronic_pain_frequency": "",
        "has_used_opioids": null,
        "opioid_use_frequency": "",
        "has_visited_er": "false",
        "er_visit_frequency": "",
        "has_been_admitted_as_inpatient": "false",
        "number_of_inpatient_admissions": "",
        "number_of_nights_as_inpatient": "",
        "days_missed": "0",
        "history_of_hepatitis_a_vaccination": "false",
        "hepatitis_a_vaccinations": [],
        "history_of_hepatitis_b_vaccination": "false",
        "hepatitis_b_vaccinations": [],
        "circumcision_performed": "false",
        "circumcision_with_excessive_bleeding": "",
        "circumcision_performed_with_clotting_factor": "",
        "iron_replacement_therapy": "",
        "menstrual_bleeding": "",
        "hysterectomy_performed": "",
        "hysterectomy_performed_date_estimation_type": "",
        "hysterectomy_performed_date_year": "",
        "hysterectomy_performed_date_month": "",
        "hysterectomy_performed_date_day": "",
        "history_of_hepatocellular_carcinoma": "No",
        "history_of_leukemia": "No",
        "history_of_other_cancer": "No",
        "history_of_fracture_of_bone": "No",
        "history_of_osteopenia": "No",
        "history_of_osteoporosis": "No",
        "history_of_acute_ischemic_stroke": "No",
        "history_of_atrial_fibrillation": "No",
        "history_of_coronary_artery_disease": "No",
        "history_of_congestive_heart_failure": "No",
        "history_of_myocardial_infarction": "No",
        "history_of_transient_ischemic_attack": "No",
        "history_of_chronic_kidney_disease": "No",
        "history_of_kidney_stones": "No",
        "history_of_nephrotic_syndrome": "No",
        "history_of_ascites": "No",
        "history_of_cirrhosis": "No",
        "history_of_hepatic_fibrosis": "No",
        "history_of_splenomegaly": "No",
        "history_of_varices": "No",
        "history_of_deep_vein_thrombosis": "No",
        "history_of_pulmonary_embolism": "No",
        "history_of_anxiety": "No",
        "history_of_depression": "No",
        "history_of_diabetes_mellitus": "No",
        "history_of_hypertension": "No",
        "history_of_iron_deficiency_anemia": "No",
        "history_of_pseudotumor": "No",
        "history_of_coronary_angioplasty": "No",
        "history_of_coronary_stent_placement": "No",
        "history_of_coronary_artery_bypass_graft": "No",
        "history_of_peritoneal_dialysis": "No",
        "history_of_hemodialysis": "No",
        "history_of_heart_valve_replacement": "No",
        "history_of_heart_valve_repair": "No",
        "history_of_kidney_transplant": "No",
        "history_of_liver_transplant": "No",
        "history_of_peripheral_revascularization": "No",
        "has_undergone_surgery": "No",
        "all_procedures_undergone_segments": {},
        "metadata": {
            "id": 20,
            "subject_id": 6,
            "study_id": 1,
            "edited_at": "2022-01-27T21:08:01.333Z",
            "created_at": "2022-01-27T21:05:31.289Z",
            "updated_at": "2022-01-27T21:08:01.353Z",
            "status": "submitted",
            "grouping": "2022-1",
            "htc_id": 353,
            "submitter_id": 2,
            "is_update": false,
            "submitted_by": "Colonel Admin",
            "deleted": false,
            "form_id": 874876025,
            "study_subject_identifier": "35M8JUAWG",
            "grouping_uuid": 359195779
        }
    }
]
 
Last edited by a moderator:
No issues for me:

C#:
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Xml;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace SimpleCS
{
    static class StringExtensions
    {
        public static string EmptyToNull(this string s)
            => String.IsNullOrEmpty(s) ? null : s;
    }

    class Program
    {
        const string Json =
@"[
    {
        'date_of_visit_year': '2017',
        'date_of_visit_month': '6',
        'date_of_visit_day': '6',
        'date_of_last_surveillance_visit_year': '2017',
        'date_of_last_surveillance_visit_month': '4',
        'date_of_last_surveillance_visit_day': '4',
        'weight': '',
        'weight_unknown': 'Unknown',
        'date_of_weight_collected_year': '',
        'date_of_weight_collected_month': '',
        'date_of_weight_collected_day': '',
        'height': '',
        'height_unknown': 'Unknown',
        'date_of_height_collected_year': '',
        'date_of_height_collected_month': '',
        'date_of_height_collected_day': '',
        'zip_code': '85950',
        'health_insurances': [
            'Indian Health Service'
        ],
        'health_insurances_segments': {
            'Indian Health Service': false
        },
        'patient_highest_education_level': 'Pre-elementary',
        'patient_grade_level': null,
        'employment_status': 'Employed full-time',
        'family_history_of_bleeding_disorder': 'false',
        'genetic_mutation': 'false',
        'htc_service_locations': [
            'HTC outreach clinic'
        ],
        'used_hemlibra_prior_to_collection': 'false',
        'cba_draw_not_available': '',
        'cba_draw_date_estimation_type': '',
        'cba_draw_date_year': '',
        'cba_draw_date_month': '',
        'cba_draw_date_day': '',
        'cba_draw_result': '',
        'cba_draw_result_other': '',
        'cba_draw_result_is_negative': '',
        'nba_draw_not_available': '',
        'nba_draw_date_estimation_type': '',
        'nba_draw_date_year': '',
        'nba_draw_date_month': '',
        'nba_draw_date_day': '',
        'nba_draw_result': '',
        'nba_draw_result_other': '',
        'nba_draw_result_is_negative': '',
        'inhibitor_not_done': 'No Results Available',
        'inhibitor_titer_draw_date_estimation_type': '',
        'inhibitor_titer_draw_date_year': '',
        'inhibitor_titer_draw_date_month': '',
        'inhibitor_titer_draw_date_day': '',
        'inhibitor_result': '',
        'inhibitor_result_not_detected': '',
        'below_inhibitor_reference_value': '',
        'highest_historic_inhibitor_titer_value': '',
        'highest_historic_inhibitor_titer_unknown': 'Unknown',
        'has_history_of_iti': 'false',
        'highest_prior_inhibitor_titer_value': '',
        'highest_prior_inhibitor_titer_unknown': '',
        'iti_use_since_last_reporting_period': [],
        'history_of_immune_modulation_therapy': 'false',
        'current_products': null,
        'current_products_unknown': 'Unknown',
        'current_treatment_regimen': 'Episodic (on-demand)',
        'use_of_clotting_factor_concentrate': '',
        'prophylaxis_start_date_known': '',
        'prophylaxis_start_date_estimation_type': '',
        'prophylaxis_start_date_year': '',
        'prophylaxis_start_date_month': '',
        'prophylaxis_start_date_day': '',
        'prophylaxis_age_estimation_type': '',
        'prophylaxis_age_unit': '',
        'prophylaxis_age_value': '',
        'joint_bleeds_before_prophylaxis': '',
        'approximate_percentage_of_missed_doses': '',
        'active_prophylaxis_medications': [],
        'use_of_clotting_factor_concentrate_unknown': null,
        'products_used_since_last_udc_visit': null,
        'products_used_unknown': 'None',
        'history_of_allergic_reaction': 'false',
        'historical_allergens': null,
        'diagnosed_with_tm_since_last_visit': 'false',
        'history_of_cvad_placement': 'false',
        'history_of_cvad_complications': 'false',
        'cvad_complications': null,
        'cvad_complications_unknown': '',
        'history_of_ich': 'false',
        'ich_events': [],
        'history_of_joint_bleeds': 'false',
        'is_first_joint_bleed_since_last_visit': '',
        'first_joint_bleed_start_date_known': '',
        'first_joint_bleed_age_estimation_type': '',
        'first_joint_bleed_age_value': '',
        'first_joint_bleed_age_unit': '',
        'first_joint_bleed_estimation_type': '',
        'first_joint_bleed_year': '',
        'first_joint_bleed_month': '',
        'first_joint_bleed_day': '',
        'number_of_bleeds_ankle_left': '',
        'number_of_bleeds_ankle_right': '',
        'number_of_bleeds_elbow_left': '',
        'number_of_bleeds_elbow_right': '',
        'number_of_bleeds_hip_left': '',
        'number_of_bleeds_hip_right': '',
        'number_of_bleeds_knee_left': '',
        'number_of_bleeds_knee_right': '',
        'number_of_bleeds_shoulder_left': '',
        'number_of_bleeds_shoulder_right': '',
        'collection_method_ankle_left': null,
        'collection_method_ankle_right': null,
        'collection_method_elbow_left': null,
        'collection_method_elbow_right': null,
        'collection_method_hip_left': null,
        'collection_method_hip_right': null,
        'collection_method_knee_left': null,
        'collection_method_knee_right': null,
        'collection_method_shoulder_left': null,
        'collection_method_shoulder_right': null,
        'number_of_bleeds_ankle_left_unknown': '',
        'number_of_bleeds_ankle_right_unknown': '',
        'number_of_bleeds_elbow_left_unknown': '',
        'number_of_bleeds_elbow_right_unknown': '',
        'number_of_bleeds_hip_left_unknown': '',
        'number_of_bleeds_hip_right_unknown': '',
        'number_of_bleeds_knee_left_unknown': '',
        'number_of_bleeds_knee_right_unknown': '',
        'number_of_bleeds_shoulder_left_unknown': '',
        'number_of_bleeds_shoulder_right_unknown': '',
        'treated_bleed_events_collection_methods': [
            'Paper Log'
        ],
        'treated_bleed_events_count': '4',
        'history_of_general_bleeds': 'No',
        'history_of_gastrointestinal_bleeds': '',
        'history_of_genitourinary_hematuria_bleeds': '',
        'history_of_genitourinary_other_bleeds': '',
        'history_of_head_extracranial_bleeds': '',
        'history_of_muscle_bleeds': '',
        'history_of_nasal_mucosa_bleeds': '',
        'history_of_oral_bleeds': '',
        'history_of_soft_tissue_bleeds': '',
        'history_of_spine_bleeds': '',
        'history_of_other_bleeds': '',
        'associated_with_gastrointestinal_bleeds': null,
        'associated_with_genitourinary_hematuria_bleeds': null,
        'associated_with_genitourinary_other_bleeds': null,
        'associated_with_head_extracranial_bleeds': null,
        'associated_with_muscle_bleeds': null,
        'associated_with_nasal_mucosa_bleeds': null,
        'associated_with_oral_bleeds': null,
        'associated_with_soft_tissue_bleeds': null,
        'associated_with_spine_bleeds': null,
        'associated_with_other_bleeds': null,
        'history_of_invasive_joint_procedure': 'No',
        'history_of_synovectomy_arthroscopic': '',
        'history_of_synovectomy_open': '',
        'history_of_synovectomy_radioisotopic': '',
        'history_of_synovectomy_unknown': '',
        'history_of_surgical_fusion_arthrodesis': '',
        'history_of_joint_replacement_arthroplasty': '',
        'history_of_other_joint_procedure': '',
        'joint_locations_synovectomy_arthroscopic': null,
        'joint_locations_synovectomy_open': null,
        'joint_locations_synovectomy_radioisotopic': null,
        'joint_locations_synovectomy_unknown': null,
        'joint_locations_surgical_fusion_arthrodesis': null,
        'joint_locations_joint_replacement_arthroplasty': null,
        'joint_locations_other_joint_procedure': null,
        'effect_on_activity_level': 'Limited school or work and limited recreational activity levels',
        'has_chronic_pain': 'false',
        'chronic_pain_frequency': '',
        'has_used_opioids': null,
        'opioid_use_frequency': '',
        'has_visited_er': 'false',
        'er_visit_frequency': '',
        'has_been_admitted_as_inpatient': 'false',
        'number_of_inpatient_admissions': '',
        'number_of_nights_as_inpatient': '',
        'days_missed': '0',
        'history_of_hepatitis_a_vaccination': 'false',
        'hepatitis_a_vaccinations': [],
        'history_of_hepatitis_b_vaccination': 'false',
        'hepatitis_b_vaccinations': [],
        'circumcision_performed': 'false',
        'circumcision_with_excessive_bleeding': '',
        'circumcision_performed_with_clotting_factor': '',
        'iron_replacement_therapy': '',
        'menstrual_bleeding': '',
        'hysterectomy_performed': '',
        'hysterectomy_performed_date_estimation_type': '',
        'hysterectomy_performed_date_year': '',
        'hysterectomy_performed_date_month': '',
        'hysterectomy_performed_date_day': '',
        'history_of_hepatocellular_carcinoma': 'No',
        'history_of_leukemia': 'No',
        'history_of_other_cancer': 'No',
        'history_of_fracture_of_bone': 'No',
        'history_of_osteopenia': 'No',
        'history_of_osteoporosis': 'No',
        'history_of_acute_ischemic_stroke': 'No',
        'history_of_atrial_fibrillation': 'No',
        'history_of_coronary_artery_disease': 'No',
        'history_of_congestive_heart_failure': 'No',
        'history_of_myocardial_infarction': 'No',
        'history_of_transient_ischemic_attack': 'No',
        'history_of_chronic_kidney_disease': 'No',
        'history_of_kidney_stones': 'No',
        'history_of_nephrotic_syndrome': 'No',
        'history_of_ascites': 'No',
        'history_of_cirrhosis': 'No',
        'history_of_hepatic_fibrosis': 'No',
        'history_of_splenomegaly': 'No',
        'history_of_varices': 'No',
        'history_of_deep_vein_thrombosis': 'No',
        'history_of_pulmonary_embolism': 'No',
        'history_of_anxiety': 'No',
        'history_of_depression': 'No',
        'history_of_diabetes_mellitus': 'No',
        'history_of_hypertension': 'No',
        'history_of_iron_deficiency_anemia': 'No',
        'history_of_pseudotumor': 'No',
        'history_of_coronary_angioplasty': 'No',
        'history_of_coronary_stent_placement': 'No',
        'history_of_coronary_artery_bypass_graft': 'No',
        'history_of_peritoneal_dialysis': 'No',
        'history_of_hemodialysis': 'No',
        'history_of_heart_valve_replacement': 'No',
        'history_of_heart_valve_repair': 'No',
        'history_of_kidney_transplant': 'No',
        'history_of_liver_transplant': 'No',
        'history_of_peripheral_revascularization': 'No',
        'has_undergone_surgery': 'No',
        'all_procedures_undergone_segments': {},
        'metadata': {
            'id': 20,
            'subject_id': 6,
            'study_id': 1,
            'edited_at': '2022-01-27T21:08:01.333Z',
            'created_at': '2022-01-27T21:05:31.289Z',
            'updated_at': '2022-01-27T21:08:01.353Z',
            'status': 'submitted',
            'grouping': '2022-1',
            'htc_id': 353,
            'submitter_id': 2,
            'is_update': false,
            'submitted_by': 'Colonel Admin',
            'deleted': false,
            'form_id': 874876025,
            'study_subject_identifier': '35M8JUAWG',
            'grouping_uuid': 359195779
        }
    }
]";

        static void UseDynamicDictionaries()
        {
            var records = JsonConvert.DeserializeObject<Dictionary<dynamic, dynamic>[]>(Json);

            var record = records.First();

            var nba_draw_result = record.ContainsKey("nba_draw_result") &&
                                  record["nba_draw_result"] != ""
                                    ? record["nba_draw_result"]
                                    : null;
            var grouping_uuid = record.ContainsKey("metadata") &&
                                record["metadata"].ContainsKey("grouping_uuid")
                                    ? record["metadata"]["grouping_uuid"]
                                    : null;
        }

        static void UseJObjects()
        {
            var records = JArray.Parse(Json);

            var record = records.First();

            var nba_draw_result = record.SelectToken("nba_draw_result")
                                        ?.Value<string>()
                                        .EmptyToNull();
            var grouping_uuid = record.SelectToken("metadata.grouping_uuid")
                                      ?.Value<int>();     
        }

        static void Main(string[] args)
        {
            UseJObjects();
            UseDynamicDictionaries();
        }
    }
}
 
Could it have anything to do with the:
C#:
            Dictionary<dynamic, dynamic>[] FWPJsonObj = JsonConvert.DeserializeObject<Dictionary<dynamic, dynamic>[]>(File.ReadAllText(JSONFile));
and the fact that I am using EF6 ?
 
Last edited by a moderator:
Back
Top Bottom