0

I am completely new to coding, bear with me if I make any stupid presumptions about this code I learnt through a youtube video. I am querying Salesforce through REST API and my response has been converted to a string. PART 1: I want to print it in pretty print JSON format and PART 2: store the JSON data into an object.

I haven't tried zilch yet but I am looking at some previous answers on this forum.

        HttpClient apiCallClient = new HttpClient();
        String restCallURL = serviceUrl + "/services/data/v45.0/query?q=SELECT+Id,PAL_ID__c+FROM+Account";
        HttpRequestMessage apirequest = new HttpRequestMessage(HttpMethod.Get, restCallURL);
        apirequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        apirequest.Headers.Add("authorization","Bearer " + authToken);
        HttpResponseMessage apiCallResponse = await apiCallClient.SendAsync(apirequest);
        String requestresponse = await apiCallResponse.Content.ReadAsStringAsync();
        Console.WriteLine(requestresponse);
        Console_textBox.AppendText(requestresponse);

Just need in Pretty Print JSON on my console and to store that data in an object.

GoldenAge
  • 2,918
  • 5
  • 25
  • 63
Raghu Rao
  • 11
  • 3
  • This question looks a little broad for this forum. You're asking two separate questions (1. Prettify; 2. Store in an object), but the format for questions on stack overflow is [one question per post](https://meta.stackexchange.com/q/222735). Also, what do you mean by "store that data in an object"? Do you want to design a c# data model corresponding to it? To parse it into some neutral data structure? Or something else? A `string` is already an `object` after all. – dbc Aug 15 '19 at 21:20
  • That being said, to prettify a JSON string see [this answer](https://stackoverflow.com/a/30329731) to [How do I get formatted JSON in .NET using C#?](https://stackoverflow.com/q/2661063). – dbc Aug 15 '19 at 21:21
  • And for "store that data in an object" you might start with [How can I parse JSON with C#?](https://stackoverflow.com/a/6620173/3744182) or [Deserializing JSON data to C# using JSON.NET](https://stackoverflow.com/a/9038318/3744182) along with [How to auto-generate a C# class file from a JSON object string](https://stackoverflow.com/q/21611674/3744182). – dbc Aug 15 '19 at 21:23
  • If you'd like to be modern use System.Text.Json instead of Json.net :) – Daniel Schmid Aug 15 '19 at 21:37

1 Answers1

2

You can make a simple class to hold the Salesforce response, like:

public class SalesforceListResponse<T>
{
    [JsonProperty("totalSize")]
    public string TotalSize { get; set; }

    [JsonProperty("done")]
    public bool Done { get; set; }

    [JsonProperty("nextRecordsUrl")]
    public string NextRecordsUrl { get; set; }

    [JsonProperty("records")]
    public T[] Records { get; set; }
}

And make a simple class to hold the account:

public class Account
{
    public string Id { get; set; }

    public string PAL_ID__c { get; set; }

    ⋮
}

Then just deserialize it:

var salesforceResponse = JsonConvert.DeserializeObject<SalesforceListResponse<Account>>(requestresponse );
crgolden
  • 4,332
  • 1
  • 22
  • 40