Tagged: kbmMW JSON
- This topic has 9 replies, 2 voices, and was last updated 5 years, 9 months ago by
David Keith.
-
AuthorPosts
-
-
March 11, 2020 at 18:21 #54548
David Keith
ParticipantHi 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.
-
March 11, 2020 at 18:36 #54549
David Keith
ParticipantI 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.
-
March 11, 2020 at 22:36 #54550
David Keith
ParticipantNever 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.
-
March 12, 2020 at 11:52 #54551
kimbomadsen
KeymasterHi,
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.
-
March 12, 2020 at 13:02 #54552
kimbomadsen
KeymasterYou can also simplify a bit what you already have.
Eg.
TkbmMWJSONObject(jsonProcesses.Values[0]).AsString[‘ProcessCode’]:=’aaaa’
-
March 12, 2020 at 20:04 #54553
David Keith
ParticipantThanks 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.
-
March 13, 2020 at 14:55 #54556
David Keith
ParticipantOkay 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.
-
March 13, 2020 at 18:51 #54557
David Keith
Participantand 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.
-
March 13, 2020 at 23:18 #54558
kimbomadsen
KeymasterHi,
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:
var dt:TkbmMWDateTime; begin dt.ISO8601String:='2020-03-13T23:09:02+01:00'; s:=dt.UTCAsFormat['Unix:%E1']; // s will contain: Unix:1584137342 end;var dt:TkbmMWDateTime; begin dt.ISO8601String:='2020-03-13T23:09:02+01:00'; s:=dt.UTCAsFormat['/Date(%E6%T1)/']; // s will contain: /Date(132279197419999988+0100)/ end;And to match your format without a timezone:
var dt:TkbmMWDateTime; begin dt.ISO8601String:='2020-03-13T23:09:02+01:00'; s:=dt.UTCAsFormat['/Date(%E6)/']; // s will contain: /Date(132279197419999988)/ end;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 5 years, 9 months ago by
kimbomadsen.
-
This reply was modified 5 years, 9 months ago by
kimbomadsen.
-
This reply was modified 5 years, 9 months ago by
-
March 16, 2020 at 17:14 #54569
David Keith
ParticipantThanks Kim! I look forward to the new capabilities!
-
This reply was modified 5 years, 9 months ago by
David Keith.
-
This reply was modified 5 years, 9 months ago by
-
-
AuthorPosts
- You must be logged in to reply to this topic.
