0

I keep trying to override values inside my JObject (i'm reciving it from web appliaction, so it can be different). I tried to use for loop, but i don't know how to iterate with JObject. Foreach is useless because i can't edit values inside my JObject.

This is my attempt:

public JObject TranslateJson(string jsonString, LanguageCharset languageCharset)
    {
      JObject json = JObject.Parse(jsonString);
      for ( int i = 0; i > json.Count; i++)
      {
        json[i].Value = "test"; //Compiler Error CS1656
      }
      return json;
    }

#Update1

public JObject TranslateJson(string jsonString, LanguageCharset languageCharset)
    {
      JObject json = JObject.Parse(jsonString);
      foreach (var x in json)
      {
        json[x.Key] = "test";
      }
      return json;
    }

#Update2

Part of json:

{
  "common": {
    "appName": "[App name] Web",
    "other": "other",
    "others": "others",
    "dateRange": {
      "start": "Date from",
      "end": "Date to"
    },
    "sidebar": {
      "dashboard": "[App name] dashboard",
      "violations": {
        "list": "Infringement list",
        "timeline": "Activities and infringements"
      },
      "drivers": "Driver list",
      "profile": "Profile",
      "settings": "Settings",
      "logout": "Sign out"
    },
    "tariff": {
      "name": {
        "CZ": "Czech tariff",
        "RO": "Romanian tariff",
        "DE": "German tariff",
        "GB": "British tariff",
        "LT": "Lithuanian tariff",
        "DK": "Danish tariff",
        "PL": "Polish tariff",
        "BE": "Belgian tariff",
        "CH": "Swiss tariff",
        "FR": "French tariff",
        "LU": "Luxembourgish tariff",
        "SE": "Swedish tariff",
        "HU": "Magyar tariff"
           [..] (more nestings)

#Update 3

Example input json:

{
  "weapons": 
  {
    "axe": "axe",
    "sword": "sword" },
  "tools":
  {
    "saw": "saw"
  }
}

Example output:

{
  "weapons": 
  {
    "axe": "translated",
    "sword": "translated" },
  "tools":
  {
    "saw": "translated"
  }
}

Why only transladed? Because when i can to overwrite value inside any nest of that json structure, i just can call my transtalion function.

Obyi
  • 617
  • 1
  • 6
  • 13
  • https://www.newtonsoft.com/json/help/html/ModifyJson.htm – Drag and Drop Apr 07 '21 at 08:45
  • Does this answer your question? [How do you Add or Update a JProperty Value in a JObject](https://stackoverflow.com/questions/30085926/how-do-you-add-or-update-a-jproperty-value-in-a-jobject) – Drag and Drop Apr 07 '21 at 08:45
  • Not at all, check my update:. – Obyi Apr 07 '21 at 09:14
  • Im puzzle by that comment. Either the doc is wrong and there is no reproduction to your error. as you can see in this demo https://dotnetfiddle.net/2zl09h. Or we are missing a [mre] – Drag and Drop Apr 07 '21 at 09:20
  • Please describe your problem in detail, and give the object you need to replace, you can provide your `json` – Yinqiu Apr 07 '21 at 09:22
  • Your missunderstanding might come from Object != JObject. You may want to parse to `JToken` and check `if (token is JArray)` or `if (token is JObject)` and have a recusive call in case of array. But that pure speculation based on my cristal ball please follow ask [ask], and [mre] by providing an Example input and expected output – Drag and Drop Apr 07 '21 at 09:27
  • I will also check the The type of the current token you are updating in order to not errase other type: `"{ 'arr1':[1,2,3] }"` => `"{ 'arr1':'test' }"` – Drag and Drop Apr 07 '21 at 09:42
  • @DragandDrop I just want to translate all values inside json from english language to my origin language by using my database, so the point to do womething like in Update3 – Obyi Apr 07 '21 at 11:15

2 Answers2

0

Below is a demo,you can check it.

var jsonString = @"
                    { items: [
                    {
                      ""name"": ""1111"", 
                      ""name2"": ""value2"", 
                      ""type"":  ""2222""
                    },
                    {
                      ""name"": ""33333"",
                      ""name2"": ""value2"",
                      ""type"": ""444444""
                    }
                    ] }";
       
        JObject json = JObject.Parse(jsonString);
       
        foreach (JObject o in json["items"].AsEnumerable())
        {
           foreach (var x in o)
            {    
               o[x.Key] = "test";        
            } 
        }
       

Test result:

enter image description here

Yinqiu
  • 6,609
  • 1
  • 6
  • 14
  • It's close, but not whats i wanted. Because 1st: i don't knwo how first node will be called (it can be items, languages or other things). 2nd, it can have multiply nodes, file for test have 11 nested nodes so i need to do it in different way. I will post json file to translate – Obyi Apr 07 '21 at 10:14
0

I tried something like that:

public string TranslateJson(string jsonString, LanguageCharset languageCharset)
    {
      JToken json = JToken.Parse(jsonString);
      foreach (var element in json)
      {
        if (element is JObject)
        {
          return "object";
        } else if (element is JArray)
        {
          return "array";
        }
      }
      return "you fckd up";
    }

And it always returning "you fckd up", but i want to get something different than it, but idk how to achive it

Obyi
  • 617
  • 1
  • 6
  • 13
  • It happens because element is always a "Property". – Obyi Apr 07 '21 at 11:12
  • Yes, element of a Token are Property. but Property.Value should be the JToken. – Drag and Drop Apr 07 '21 at 11:35
  • @DragandDrop Then when i try to do if (element.Value is JToken) then in this line i got error: https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0837?f1url=%3FappId%3Droslyn%26k%3Dk(CS0837) – Obyi Apr 07 '21 at 11:40
  • https://stackoverflow.com/questions/57322438/getting-all-the-properties-with-a-name-from-a-json-and-changing-the-value may help. To get the type you can also use type property and compare to jtoken type enum. – Drag and Drop Apr 07 '21 at 12:08