Search code examples
c#jsonjson.net

How to delete header name of Json data in C#


I am trying to delete this header "vendor": and { } branch from Json formated output.

The reason to delete is to map these three fields names ("RECORDNO".."NAME") to SQL table.

enter image description here

Bottom is expected output:

enter image description here

Bottom is C# code that iterates thru each ones and populates the result, and later uses JsonCovert to format output in Json format.

public static string Run(ILogger logger)       
    {
        OnlineClient client = Bootstrap.Client(logger);
        ReadByQuery query = new ReadByQuery()
        {
            ObjectName = "VENDOR",
            PageSize = 600, 
            Fields =
            {
                "RECORDNO",
                "VENDORID",
                "NAME"
            }
        };
        logger.LogInformation("Executing query to Intacct API");
        Task<OnlineResponse> task = client.Execute(query);
        task.Wait();            
        OnlineResponse response = task.Result;
        Result result = response.Results[0];
        LogManager.Flush();
        int i = 1;
        while (result.NumRemaining > 0 && i <= 1 && !string.IsNullOrEmpty(result.ResultId))
            {
            i++;
            ReadMore more = new ReadMore()
            {
                ResultId = result.ResultId
            };
            Task<OnlineResponse> taskMore = client.Execute(more);
            taskMore.Wait();
            OnlineResponse responseMore = taskMore.Result;                
            Result resultMore = responseMore.Results[0];
            dynamic resultJson =   
                   JsonConvert.DeserializeObject(JsonConvert.SerializeObject(result.Data));  
            string resultJsonString = resultJson.ToString();  
            return resultJsonString;
        }
        return "";
    }

I am not fluent in C# so I am not sure how to express to delete the "vendor:" portion from 'query'.

I am sure there are a lot of unnecessary lines from this C# code which could be deleted (to clean up).

What is expression to delete "vendor": and { } branch?


This is updated code based on the comment from @Serge.

int i = 1;
        while (result.NumRemaining > 0 && i <= 1 && !string.IsNullOrEmpty(result.ResultId))
            {
            i++;
                            
            dynamic resultJson =   
                   JsonConvert.DeserializeObject(JsonConvert.SerializeObject(result.Data));  
            string resultJsonString = resultJson.ToString();

            var jsonArray = JArray.Parse(resultJsonString);
            var newJsonArray = jsonArray.SelectTokens("$..VENDOR");
            var result1 = JsonConvert.SerializeObject(newJsonArray, Newtonsoft.Json.Formatting.Indented);

            return result1;

        }

When I modified like this way, I got no data output (as shown below).

[ ]

What is proper way (area) to put the bottom code?

var jsonArray=JArray.Parse(json);  

var newJsonArray =  jsonArray.SelectTokens("$..VENDOR");
    
var result= JsonConvert.SerializeObject(newJsonArray, Newtonsoft.Json.Formatting.Indented);


Solution

  • try this using Newtonsoft.Json

    using Newtonsoft.Json;
    ....
    
    var json="[{\"VENDOR\":{\"RECORDNO\":\"1\",\"VENDORID\":\"ID1\",\"NAME\":\"Name1\"}},{\"VENDOR\":{\"RECORDNO\":\"2\",\"VENDORID\":\"ID2\",\"NAME\":\"Name2\"}},{\"VENDOR\":{\"RECORDNO\":\"3\",\"VENDORID\":\"ID3\",\"NAME\":\"Name3\"}}]"
    
     var jsonArray=JArray.Parse(json);  
    
     var newJsonArray =  jsonArray.SelectTokens("$..VENDOR");
            
     var result= JsonConvert.SerializeObject(newJsonArray, Newtonsoft.Json.Formatting.Indented);
    

    result

      [ {
        "RECORDNO": "1",
        "VENDORID": "ID1",
        "NAME": "Name1"
      },
      {
        "RECORDNO": "2",
        "VENDORID": "ID2",
        "NAME": "Name2"
      },
      {
        "RECORDNO": "3",
        "VENDORID": "ID3",
        "NAME": "Name3"
      } ]
    

    or you can create classes and have typed c# list

    var jsonDeserialized=JsonConvert.DeserializeObject<VendorRoot[]>(json);
    
    List<VENDOR> newJsonList = jsonDeserialized.Select(d => d.VENDOR ).ToList();
    
    var result= JsonConvert.SerializeObject(newJsonList,  Newtonsoft.Json.Formatting.Indented);
    

    classes

    
    public class VendorRoot
    {
        public VENDOR VENDOR { get; set; }
    }
    public class VENDOR
    {
        public string RECORDNO { get; set; }
        public string VENDORID { get; set; }
        public string NAME { get; set; }
    }