Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to change a key value pair within a nested json structure C#

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

// Read the JSON file. 
var json = JObject.Parse(File.ReadAllText("input.json"));

// Extract label by GUID. Note that GUID case is inconsistent within the file, so we force upper case.
var labelByGuid = json.SelectTokens("cases.procedure.version.forms[*].sections[*].rows[*].fields[?(@.guid && @.label)]")
                      .ToDictionary(t => t["guid"]!.ToString().ToUpperInvariant(),
                                    t => t["label"]!.ToString());

// Update entries.
foreach (var entry in json.SelectTokens("cases.content[?(@.field)]"))
{
    var field = entry["field"]!.ToString().ToUpperInvariant();
    if (!labelByGuid.TryGetValue(field, out var label))
        continue;
    
    entry["field"] = label;
}

// Write back the JSON object.
var settings = new JsonSerializerSettings { Formatting = Formatting.Indented };
var serializer = JsonSerializer.Create(settings);
await using var outputStream = File.CreateText(@"output.json");
serializer.Serialize(outputStream, json);
Comment

PREVIOUS NEXT
Code Example
Javascript :: track call recording in facebook using elements 
Javascript :: react select disable 
Javascript :: react open popup to upload image file 
Javascript :: fireOnChange 
Javascript :: node-mongodb-native keep collection 
Javascript :: request submit form 
Javascript :: yaml request body json 
Javascript :: regex online converter 
Javascript :: Using Bind() With BuiltIn JavaScript Function 
Javascript :: Creating Variables In Self Evoking Function 
Javascript :: audio js fast 
Javascript :: leap year list 
Javascript :: Saving dependencies in your node package.json syntax 
Javascript :: controllare che ci sia un file in javascript 
Javascript :: what is setImmediate vs clearImmediate 
Javascript :: Javascript Encapsulation Inheritance Polymorphism Composition 
Javascript :: react sate and props 
Javascript :: React Native - iOS Release build crashing 
Javascript :: como hacer un contador de tiempo en javascript 
Javascript :: what does the symbol function do in javascript 
Javascript :: ahead-of-time (AOT) compilation 
Javascript :: load limited data and search data from all in angularjs 
Javascript :: winston transport file another directory 
Javascript :: &quot in json 
Javascript :: base64-XMLHttpRequest 
Javascript :: create number format excel react native 
Javascript :: js onclick add table row 
Javascript :: redirect in react-router-dom v6 
Javascript :: react onsubmit get form values 
Javascript :: vuejs methods 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =