How to edit JSON packet?

Home Forums kbmMW How to edit JSON packet?

Tagged: 

Viewing 9 reply threads
  • Author
    Posts
    • #54548
      David Keith
      Participant

      Hi Kim!

      I have previously used TkbmMWJSONStreamer and TkbmMWJSONObject to read an inbound JSON packet and select values from the packet, worked well.

      Now I’m trying to :

      1) Load a predefined JSON template

      2) Edit the values in name value pairs in the JSON template

      I looked at your ObjectNotation demo but could not figure out how to do this. Below is a sterilized version of the JSON template:

      KJSONTemplate: UnicodeString =
      ‘{‘ +
      ‘”topLevelProperty”:{‘ +
      ‘”origin”:”^origin^”,’ +
      ‘”AnId”:”^anid^”,’ +
      ‘”User”:{‘ +
      ‘”UserName”:”^UserName^”,’ +
      ‘”Password”:”APassword”‘ +
      ‘},’ +
      ‘”Order”:{‘ +
      ‘”anotherid”:null,’ +
      ‘”order_id”:”^orderid^”,’  +
      ‘”order_created_on”:”^ordcreatedate^”,’ +
      ‘”Processes”:[‘ +
      ‘{‘ +
      ‘”ProcessCode”:”^processcode^”,’ +
      ‘”ProcessDesc”:”^processdesc^”‘ +

      contrast”‘ +
      ‘}’ +
      ‘],’ +
      ‘”person”:{‘ +
      ‘”id”:”^Id^”,’ +
      ‘”first_name”:”^firstname^”,’ +
      ‘”last_name”:”^lastname^”,’ +
      ‘”dob”:”^dob^”,’ +
      ‘”age”:”^age^”,’ +
      ‘”gender”:”^gender^”,’ +
      ‘”IsPregnant”:null’ +
      ‘}’ +
      ‘},’ +
      ‘”UserInfo”:{‘ +
      ‘”user”:”^user^”,’ +
      ‘”token”:”^token^”,’ +

      ‘”location”:”^location^”,’ +
      ‘”customer”:”^customer^”,’ +
      ‘”performer”:{‘ +
      ‘”UID”:”^UID^”,’ +
      ‘”Name”:”^name^”,’ +
      ‘”StreetAddressLine1″:”^streetaddr^”,’ +
      ‘”City”:”^city^”,’ +
      ‘”State”:”^state^”,’ +
      ‘”PostalCode”:”^zip^”,’ +
      ‘”Country”:”^country^”,’ +
      ‘”Telephone”:”^telo^”,’ +
      ‘”Specialty”:{‘ +
      ‘”Description”:”^aspeciality^”‘ +
      ‘}’ +
      ‘}’ +
      ‘}’ +
      ‘}’ +
      ‘}’;

      I do not have a need to convert this to an object nor transform this to a different format, I just want to load the template, edit the values and save/send the modified JSON packet.

      FWIW it doesn’t look easy to do with TJSONObject either, that was not at all clear how to do this.

    • #54549
      David Keith
      Participant

      I tried to clean this up a bit but it didn’t post for some reason.

      Among other things, please remove the line that reads

      ‘contrast”‘ +’

      from the JSON packet.

       

    • #54550
      David Keith
      Participant

      Never mind, I think I have this figured out. Very verbose code requirements.

      json := TkbmMWJSONObject(stream.LoadFromUTF16String(ajsontemplatestring)

      jsonBase := TkbmMWJSONObject(json.PropertyValue[0]);

      TkbmMWJSONNative(jsonBase.PropertyValueByName[‘origin’]).AsString := origin;

      jsonProcesses := TkbmMWJSONArray(jsonOrder.AsArray[‘Processes’]);

      kbmMWJSONNative(TkbmMWJSONObject(jsonProcesses.Values[0]).PropertyValueByName[‘ProcessCode’]).AsString := ProcessCode;

      etc.

      Thanks.

    • #54551
      kimbomadsen
      Keymaster

      Hi,

      Or you could make an XSD schema out of your template and import it using the kbmMW XSD schema importer. The generated classes can be used for marshalling and unmarshalling your JSON too, so you would have a syntax checked template that way.

      Or use the kbmMW automatic schema detection stuff as explained here:
      https://components4developers.blog/2019/03/11/rest-easy-with-kbmmw-24-xml_json_yaml_to_object_conversion/

      You could ofcourse also just make the classes yourself.

    • #54552
      kimbomadsen
      Keymaster

      You can also simplify a bit what you already have.

      Eg.

      TkbmMWJSONObject(jsonProcesses.Values[0]).AsString[‘ProcessCode’]:=’aaaa’

    • #54553
      David Keith
      Participant

      Thanks for the response Kim.

      Just out of curiosity, does your JSON processor know anything about this date format?:

      “Expired”:”\/Date(1584031516248)\/”

      Is this a “native” JSON date time type that you know how to process?

      Thanks again.

    • #54556
      David Keith
      Participant

      Okay it looks like this is a Unix time epoch. I used System.DateUtils.UnixToDateTime to convert it. Unfortunately the conversion produces the wrong date with a year of 52166… probably an issue at the source.

    • #54557
      David Keith
      Participant

      and the reason is…. the JSON \/Date(1234567891012)\/ is in milliseconds, not seconds. So the result that is returned from calling System.DateUtils.UnixToDateTime should be divided by 1000.

    • #54558
      kimbomadsen
      Keymaster

      Hi,

      JSON do not define what a date/time value is. However the defacto standard is to adhere to the ISO8601 format.

      Unfortunately Microsoft as usual, has made their own off standard solution, which is used until .Net v. 4.5 as far as I remember, which produce that funny looking date/time value.

      It is a FileTime, not a Unix/Posix epoch.
      It can also include a timezone, although your example do not.

      Next version of kbmMW will, triggered by your question, contain extensive epoch handling in the TkbmMWDateTime record.

      Hence you will be able to do like this:

      And to match your format without a timezone:

      Similarly one can do:
      dt.UTCAsFormat['/Date(%E6)/']:=s;

      Which result in your date being correctly converted to a TkbmMWDateTime value.

      This can be taken advantage of in the kbmMWJSON parser/generator too, since you can get/set datetime values using the GetAsFmtDateTime/SetAsFmtDateTime method, which will take a format string as shown above.

      • This reply was modified 3 weeks, 1 day ago by kimbomadsen.
      • This reply was modified 3 weeks, 1 day ago by kimbomadsen.
    • #54569
      David Keith
      Participant

      Thanks Kim! I look forward to the new capabilities!

      • This reply was modified 2 weeks, 6 days ago by David Keith.
Viewing 9 reply threads
  • You must be logged in to reply to this topic.