skip to Main Content

I have following JSON format. I want to make images json object to json array. Means want to convert json object to json array.

This is my current JSON

"data": [
        {
            "id": "testid",
            "variants": [
                {
                    "name": "testname",
                    "description": "testdesc",
                    "images": {
                        "2T": [
                            {
                                "id": testid,
                                "product_id": "testid1"
                            }
                        ]
                    }
                }
            ],
        }
        ]

I want to new JSON to look like this.

"data": [
        {
            "id": "testid",
            "variants": [
                {
                    "name": "testname",
                    "description": "testdesc",
                    "images": [ // here I want to convert to json array 
                        "2T": [
                            {
                                "id": testid,
                                "product_id": "testid1"
                            }
                        ]
                    ]
                }
            ],
        }
        ]

2

Answers


  1. As far I got your point:
    You may want to convert the JSON object to a JSON array in C#, you can use the JArray class from the Newtonsoft.Json.Linq namespace. Here’s an example:

    using Newtonsoft.Json.Linq;
    
    JObject obj = JObject.Parse(@"{
      'data': [
        {
          'id': 'testid',
          'variants': [
            {
              'name': 'testname',
              'description': 'testdesc',
              'images': {
                '2T': [
                  {
                    'id': 'testid',
                    'product_id': 'testid1'
                  }
                ]
              }
            }
          ]
        }
      ]
    }");
    
    JArray arr = new JArray(obj["data"][0]["variants"][0]["images"]["2T"]);
    
    obj["data"][0]["variants"][0]["images"] = arr;
    
    Console.WriteLine(obj.ToString());
    

    This will output:

    {
      "data": [
        {
          "id": "testid",
          "variants": [
            {
              "name": "testname",
              "description": "testdesc",
              "images": [
                {
                  "id": "testid",
                  "product_id": "testid1"
                }
              ]
            }
          ]
        }
      ]
    }
    
    Login or Signup to reply.
  2. Json string you want is not valid , but you can try this code to get a valid json with image array

        var jObj = JObject.Parse(json);
    
        var variants =jObj["data"].SelectMany(d => d["variants"]); 
        foreach (var variant in variants)
        {
            var newArr = new JArray();
            foreach (var prop in ((JObject)variant["images"]).Properties())
                newArr.Add(new JObject { ["Code"] = prop.Name, ["Data"] = prop.Value });
            variant["images"] = newArr;
        }
    
        json = jObj.ToString();
    

    output

    {
      "data": [
        {
          "id": "testid",
          "variants": [
            {
              "name": "testname",
              "description": "testdesc",
              "images": [
                {
                  "Code": "2T",
                  "Data": [
                    {
                      "id": "testid",
                      "product_id": "productid1"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search