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.
Bottom is expected output:
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?
Thank you.
CodePudding user response:
You could create a new JObject assigning to it the JObject value available at the "vendor" field.
You could do something like this:
JObject changed = jobj["vendor"].Value<JObject>();
Here is an example:
JObject toChange = new JObject();
toChange["vendor"] = new JObject();
toChange["vendor"]["fieldA"] = "value";
toChange["vendor"]["fieldB"] = "value";
JObject changed = toChange["vendor"].Value<JObject>();
Console.WriteLine(toChange.ToString());
Console.WriteLine(changed.ToString());
FROM THIS:
{
"vendor": {
"fieldA": "value",
"fieldB": "value"
}
}
YOU'D GET THIS:
{
"fieldA": "value",
"fieldB": "value"
}
CodePudding user response:
try this using Newtonsoft.Json
using Newtonsoft.Json;
....
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; }
}