1

I have a JSON string that is made up of a JSON wrapper and within this wrapper is JSON payload.

I need to beautify the string so it is more readable.

I have tried using some regular beautifiers (Newtonsoft, plus a nice one I found on SO) however because of the way this JSON string is given to me they do not work

Here is my JSON string

{ "LocalReferenceNumber": "DNXLHR1906000000000005", "DeclarationStatus": "Submitted", "Payload": "{\"DeclarationType\":null,\"AcceptanceDateUtc\":null,\"DeclarationUcr\":\"9GB949032610000-AI-000000031-ASH\",\"LocalReferenceNumber\":\"DNXLHR1906000000000005\",\"TraderReference\":\"AI-000000031-ASH\",\"Exporter\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"Importer\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"Seller\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"Buyer\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"Declarant\":{\"IdentificationNumber\":\"GB949032610000\",\"Name\":\"ASM (UK) LTD\",\"Street\":\"ASHFORD HOUSE\",\"City\":\"ASHFORD\",\"PostCode\":\"TW15 2TQ\",\"Country\":\"GB\"},\"Representative\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"Representation\":0,\"TransportTypeOnArrival\":null,\"TransportIdentityOnArrival\":null,\"BorderTransportMode\":4,\"TransportCountryAtBorder\":null,\"InlandTransportMode\":null,\"TotalPackages\":null,\"DispatchCountry\":null,\"DestinationCountry\":null,\"InvoiceCurrency\":null,\"InvoiceTotal\":null,\"DeliveryTerms\":null,\"ExchangeRate\":null,\"NatureOfTransaction\":null,\"GoodsLocation\":{\"CountryCode\":null,\"Type\":null,\"Quantifier\":null,\"Identification\":null,\"Name\":null},\"FirstDeferment\":null,\"SecondDeferment\":null,\"WarehouseIdentity\":null,\"WarehouseType\":null,\"SupervisingOffice\":null,\"AirportOfLoading\":null,\"MovementReferenceNumber\":null,\"AuthorisationHolders\":[],\"Containers\":[],\"Guarantees\":[],\"AdditionalFiscalReferences\":[],\"AdditionalSupplyChainActors\":[],\"PreviousDocuments\":[{\"Category\":\"Z\",\"Type\":\"DCR\",\"Reference\":\"9GB949032610000-AI-000000031-ASH\",\"Identifier\":null,\"Order\":1}],\"Items\":[{\"ItemNumber\":1,\"DeclarationUcr\":null,\"TraderReference\":null,\"CountryOfOrigin\":null,\"PreferentialCountryOfOrigin\":null,\"Preference\":null,\"Quota\":null,\"GoodsDescription\":\"DSAFASDFSA\",\"ItemPrice\":null,\"ItemCurrency\":null,\"StatisticalValue\":null,\"StatisticalValueCurrency\":null,\"NetMass\":null,\"GrossMass\":null,\"SupplementaryUnits\":null,\"ValuationMethod\":null,\"ValuationIndicators\":null,\"DispatchCountry\":null,\"DestinationCountry\":null,\"NatureOfTransaction\":null,\"Exporter\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"Seller\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"Buyer\":{\"IdentificationNumber\":null,\"Name\":null,\"Street\":null,\"City\":null,\"PostCode\":null,\"Country\":null},\"CustomsUnionAndStatisticsNumber\":null,\"CommodityCode\":null,\"ProcedureCode\":null,\"CommodityAdditionalCodes\":[],\"AdditionalProcedureCodes\":[],\"PreviousDocuments\":[],\"Containers\":[],\"Packages\":[],\"FiscalReferences\":[],\"SupplyChainActors\":[],\"AdditionsAndDeductions\":[],\"AdditionalInformation\":[],\"DocumentCertificateAuthorisationReferences\":[],\"TaxLines\":[]}],\"OtherAdditionsAndDeductions\":[]}", "SubmittingBy": "devbuilder", "SubmittedOn": "2019-06-14T09:08:42.788Z" }

As you can see there is a 'Payload' section that contains JSON data within the 'JSON wrapper' I need to format the whole string so it looks like formatted JSON data, i.e proper indents and the like.

Ross
  • 405
  • 3
  • 8
  • 19
  • Have you seen this [answer](https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity)? Look for "prettyprint", maybe it helps. – Daniel Aug 08 '19 at 15:57
  • 3
    The issue is even though the contents of Payload is technically in JSON format, its really just a string value like any other value. You would have to pull out the Payload object from the JSON into its own json object, prettify it, then place it back into the whole json. – DetectivePikachu Aug 08 '19 at 16:00
  • Does the outer JSON wrapper have a fixed schema? – dbc Aug 08 '19 at 19:47
  • The outer JSON has a fixed schema – Ross Aug 09 '19 at 07:34

1 Answers1

2

Ended up stripping out the payload from the string:

string payloadPropertyName = "\"Payload\":\"";
        int startIndex = rawMessage.IndexOf(payloadPropertyName, StringComparison.Ordinal) + payloadPropertyName.Length;
        int endIndex = rawMessage.IndexOf("\",\"SubmittingBy\"", StringComparison.Ordinal);

        string payload = rawMessage.Substring(startIndex, endIndex - startIndex);
        payload = payload.Replace("\\\"", "\"");

        return Newtonsoft.Json.Linq.JToken.Parse(payload).ToString(Newtonsoft.Json.Formatting.Indented);

And beautifying: Newtonsoft.Json.Linq.JToken.Parse(payload).ToString(Newtonsoft.Json.Formatting.Indented);

Then I used JsonDeserialize to deserialize my wrapper JSON data into an object

jsonSerializer.JsonDeserialize<Wrapper>(rawMessage);


 internal class Wrapper
{
    public string LocalReferenceNumber { get; set; }

    public string DeclarationStatus { get; set; }

    public string SubmittingBy { get; set; }

    public DateTime SubmittedOn { get; set; }
}

Finally I just formatted the Wrapper data:

Local Reference Number: DNXLHR1906000000000005

Declaration Status: Submitted

Submitting By: devbuilder

Submitted On: "2019-06-14 09:08:42

Followed by my beautified JSON payload

Bit of a workaround but works just as well in my case.

Ross
  • 405
  • 3
  • 8
  • 19