0

I'm having a List of JsonElement, and I want to set the value of each element property through iteration but I don't know how to proceed with a JsonElement object.

Here my code :

List<JsonElement> exportExported = GetDatas();
foreach (JsonElement export in exportExported)
{
    
    //here setting the value of an export. Example : export.reference = "test";
    export.GetProperty("reference") = "test" //does not work 
    
    System.Threading.Thread.Sleep(2500);
}

Thanks for clues

harili
  • 406
  • 9
  • 24
  • 1
    Check whether something on [this answer](https://stackoverflow.com/questions/72732311/how-do-you-change-the-value-of-a-jsonelement) can help you. – Rogerson Nazário Feb 22 '23 at 10:14
  • 1
    please check it here https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/customize-properties?pivots=dotnet-7-0, hope it solve your problem – nohattee Feb 22 '23 at 10:17
  • 1
    You can't, `JsonElement` is immutable. You need `JsonNode` (or your own custom class). – Jeroen Mostert Feb 22 '23 at 10:17
  • 1
    JsonElement is not editable from there. There are many other ways to do that. If you provide a bit more info about what you're trying to achieve, it will be easier to provide a solution. Is the structure of you exported data always the same? Can you please provide an example of the exported data json? – Just Shadow Feb 22 '23 at 10:18
  • @JustShadow My Json structure is a basic json array : [{"name":"Jones","reference":"6546"},{"name":"James","reference":"9631"},{"name":"Rita","reference":"8979"}] – harili Feb 22 '23 at 10:27
  • @JeroenMostert Alright, thanks for that information. So I need other steps before the iteration – harili Feb 22 '23 at 10:32
  • @JustShadow And the structure is always the same and I'm just trying to set the property "reference" for every single object in the array. – harili Feb 22 '23 at 10:34

1 Answers1

1

Mutating JsonElement would not work.
In case you want everything to be done explicitly, you might consider deserializing the json, editing and serializing back if needed.
This way you'll also be sure that the incoming data always has the structure you need

using System;
using System.Collections.Generic;
using System.Text.Json;

namespace Deserializer
{
    // This is your custom structure. I've called it UserInfo, but feel free to rename it
    public class UserInfo
    {
        public string Name { get; set; }
        public string Reference { get; set; }
    }

    public class Program
    {
        public static void Main()
        {
            // Sample data
            string jsonString =
            @"[{""name"":""Jones"",""reference"":""6546""},{""name"":""James"",""reference"":""9631""},{""name"":""Rita"",""reference"":""8979""}]";
                
            // Deserializing the json into a list of objects
            var userInfoList = JsonSerializer.Deserialize<List<UserInfo>>(jsonString);
            
            foreach (var userInfo in userInfoList) {
                // Modifying the data
                userInfo.Reference = "test";
            }

            // Converting back to json string in case it's needed.
            var modifiedJsonString = JsonSerializer.Serialize(userInfoList);

            Console.WriteLine(modifiedJsonString);
        }
    }
}
Just Shadow
  • 10,860
  • 6
  • 57
  • 75
  • That's what I did finally ahah thanks again. But I didn't create a class structure (not my choice because intership) and did deserialize to a dynamic type to access to the mutable properties of my array :) – harili Feb 23 '23 at 11:07