grantful 3 Posted June 3, 2023 I am working with an api to get some data. https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/1G1BE5SM8J7207504?format=json&modelyear So when i post to this and get the returned info as json i need to pull out the Results I have used the Delphi Rest Debuggr and i see the info i need when i choose the string and press apply I have copied the components to my project and when i right click the memory table and choose edit Dataset. I see the jason string with the Results How can i drill down to { "Value": "CHRYSLER", "ValueId": "477", "Variable": "Make", "VariableId": 26 }, { "Value": "CHRYSLER DE MEXICO TOLUCA", "ValueId": "1002", "Variable": "Manufacturer Name", "VariableId": 27 }, { "Value": "PT Cruiser", "ValueId": "3595", "Variable": "Model", "VariableId": 28 }, { "Value": "2003", "ValueId": "", "Variable": "Model Year", "VariableId": 29 }, Thanks for any help i have never wroked with Json before Thanks for any help. Share this post Link to post
programmerdelphi2k 237 Posted June 3, 2023 uses System.JSON; procedure YourProcedure; var jsonArray: TJSONArray; jsonObject: TJSONObject; begin jsonArray := TJSONArray.Create; // Add each object to the JSON array jsonObject := TJSONObject.Create; jsonObject.AddPair('Value', 'CHRYSLER'); jsonObject.AddPair('ValueId', '477'); jsonObject.AddPair('Variable', 'Make'); jsonObject.AddPair('VariableId', '26'); jsonArray.AddElement(jsonObject); jsonObject := TJSONObject.Create; jsonObject.AddPair('Value', 'CHRYSLER DE MEXICO TOLUCA'); jsonObject.AddPair('ValueId', '1002'); jsonObject.AddPair('Variable', 'Manufacturer Name'); jsonObject.AddPair('VariableId', '27'); jsonArray.AddElement(jsonObject); jsonObject := TJSONObject.Create; jsonObject.AddPair('Value', 'PT Cruiser'); jsonObject.AddPair('ValueId', '3595'); jsonObject.AddPair('Variable', 'Model'); jsonObject.AddPair('VariableId', '28'); jsonArray.AddElement(jsonObject); jsonObject := TJSONObject.Create; jsonObject.AddPair('Value', '2003'); jsonObject.AddPair('ValueId', ''); jsonObject.AddPair('Variable', 'Model Year'); jsonObject.AddPair('VariableId', '29'); jsonArray.AddElement(jsonObject); // Convert the JSON array to a string ShowMessage(jsonArray.ToString); jsonArray.Free; end; here you had a JSONArray with 4 elements. each element has 4 pairs (key:value). some like this. now see on help to "Get" the values for i := 0 to jsonArray.Count - 1 do begin jsonObject := jsonArray.Items[i] as TJSONObject; if jsonObject.GetValue('Variable').Value = 'Model' then begin // Found the object with "Model" variable, get the "Value" value ShowMessage(jsonObject.GetValue('Value').Value); Break; end; end; 1 Share this post Link to post
grantful 3 Posted June 3, 2023 Hey thanks for the great example. I will look at this later. Share this post Link to post
mytbo 5 Posted June 3, 2023 7 hours ago, grantful said: How can i drill down to With mORMot you can do it very easy. uses mormot.core.base, mormot.core.json, mormot.net.client; type TDataRec = packed record Count: Integer; Message: RawUtf8; SearchCriteria: RawUtf8; Results: array of record Value: RawUtf8; ValueId: Integer; Variable: RawUtf8; VariableId: Integer; end; end; var url: RawUtf8 := 'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/1G1BE5SM8J7207504?format=json&modelyear'; var content: RawByteString := HttpGet(url); if content <> '' then begin var dataRec: TDataRec; if RecordLoadJson(dataRec, content, TypeInfo(TDataRec)) then begin ShowMessage(dataRec.Count.ToString); ShowMessage(Length(dataRec.Results).ToString); for var i: Integer := 0 to High(dataRec.Results) do begin if dataRec.Results[i].Variable = 'Crash Imminent Braking (CIB)' then ShowMessage(dataRec.Results[i].VariableId.ToString); end; end; end; Additional information can be found in this article in Delphi-PRAXIS forum. Here is the translation into English with Google Translator. The result is not perfect (rather not so good), also some formatting is destroyed, but it is readable. With best regards Thomas Share this post Link to post
grantful 3 Posted June 3, 2023 59 minutes ago, mytbo said: With mORMot you can do it very easy. uses mormot.core.base, mormot.core.json, mormot.net.client; type TDataRec = packed record Count: Integer; Message: RawUtf8; SearchCriteria: RawUtf8; Results: array of record Value: RawUtf8; ValueId: Integer; Variable: RawUtf8; VariableId: Integer; end; end; var url: RawUtf8 := 'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/1G1BE5SM8J7207504?format=json&modelyear'; var content: RawByteString := HttpGet(url); if content <> '' then begin var dataRec: TDataRec; if RecordLoadJson(dataRec, content, TypeInfo(TDataRec)) then begin ShowMessage(dataRec.Count.ToString); ShowMessage(Length(dataRec.Results).ToString); for var i: Integer := 0 to High(dataRec.Results) do begin if dataRec.Results[i].Variable = 'Crash Imminent Braking (CIB)' then ShowMessage(dataRec.Results[i].VariableId.ToString); end; end; end; Additional information can be found in this article in Delphi-PRAXIS forum. Here is the translation into English with Google Translator. The result is not perfect (rather not so good), also some formatting is destroyed, but it is readable. With best regards Thomas Thanks very much for the help. I will look at this. Share this post Link to post
grantful 3 Posted June 11, 2023 On 6/3/2023 at 4:26 PM, mytbo said: With mORMot you can do it very easy. uses mormot.core.base, mormot.core.json, mormot.net.client; type TDataRec = packed record Count: Integer; Message: RawUtf8; SearchCriteria: RawUtf8; Results: array of record Value: RawUtf8; ValueId: Integer; Variable: RawUtf8; VariableId: Integer; end; end; var url: RawUtf8 := 'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/1G1BE5SM8J7207504?format=json&modelyear'; var content: RawByteString := HttpGet(url); if content <> '' then begin var dataRec: TDataRec; if RecordLoadJson(dataRec, content, TypeInfo(TDataRec)) then begin ShowMessage(dataRec.Count.ToString); ShowMessage(Length(dataRec.Results).ToString); for var i: Integer := 0 to High(dataRec.Results) do begin if dataRec.Results[i].Variable = 'Crash Imminent Braking (CIB)' then ShowMessage(dataRec.Results[i].VariableId.ToString); end; end; end; Additional information can be found in this article in Delphi-PRAXIS forum. Here is the translation into English with Google Translator. The result is not perfect (rather not so good), also some formatting is destroyed, but it is readable. With best regards Thomas Thanks for your help. I have one compiler error i can not figure out. [dcc32 Error] MainTab.pas(124): E2026 Constant expression expected var Form3: TForm3; url: RawUtf8 = 'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/1G1BE5SM8J7207504?format=json&modelyear'; content: RawByteString = HttpGet(url); <<<<<<<<<<<<<<<Here at this line is the error. Thanks for helping me with this Share this post Link to post
grantful 3 Posted June 11, 2023 I have changed the code to procedure TForm1.SpeedButton1Click(Sender: TObject); begin const content: RawByteString = HttpGet(url); if content <> '' then begin var dataRec: TDataRec; if RecordLoadJson(dataRec, content, TypeInfo(TDataRec)) then begin ShowMessage(dataRec.Count.ToString); ShowMessage(Length(dataRec.Results).ToString); for var i: Integer := 0 to High(dataRec.Results) do begin if dataRec.Results.Variable = 'Crash Imminent Braking (CIB)' then ShowMessage(dataRec.Results.VariableId.ToString); end; end; end; end; it seems to work Share this post Link to post
grantful 3 Posted June 11, 2023 The mormot solution only works for windows 😞 Share this post Link to post
grantful 3 Posted June 11, 2023 (edited) I can show the tubular data in the memorytable uJasonDataModule.RESTRequest1.Execute(); edtModel.Text := uJasonDataModule.FDMemTable1.FieldByName('Results').AsString; How can i get to the info in the results json ? { "Count": 136, "Message": "Results returned successfully. NOTE: Any missing decoded values should be interpreted as NHTSA does not have data on the specific variable. Missing value should NOT be interpreted as an indication that a feature or technology is unavailable for a vehicle.", "SearchCriteria": "VIN:1gt125e85df163635", "Results": [ { "Value": "", "ValueId": "", "Variable": "Suggested VIN", "VariableId": 142 }, { "Value": "1", "ValueId": "1", "Variable": "Error Code", "VariableId": 143 }, { "Value": "", "ValueId": "", "Variable": "Possible Values", "VariableId": 144 }, { "Value": null, "ValueId": "", "Variable": "Additional Error Text", "VariableId": 156 }, { "Value": "1 - Check Digit (9th position) does not calculate properly", "ValueId": "", "Variable": "Error Text", "VariableId": 191 }, { "Value": "1GT125E8*DF", "ValueId": "", "Variable": "Vehicle Descriptor", "VariableId": 196 }, { "Value": null, "ValueId": null, "Variable": "Destination Market", "VariableId": 10 }, { "Value": "GMC", "ValueId": "472", "Variable": "Make", "VariableId": 26 }, { "Value": "GENERAL MOTORS LLC", "ValueId": "984", "Variable": "Manufacturer Name", "VariableId": 27 }, { "Value": "Sierra", "ValueId": "1857", "Variable": "Model", "VariableId": 28 }, { "Value": "2013", "ValueId": "", "Variable": "Model Year", "VariableId": 29 }, This is what i am getting with uJasonDataModule.RESTRequest1.Execute(); edtModel.Text := uJasonDataModule.FDMemTable1.FieldByName('Results').AsString; Any help is greatly. appreciated Edited June 11, 2023 by grantful Share this post Link to post
programmerdelphi2k 237 Posted June 12, 2023 (edited) @grantful a FDMemTable can load data from many type, including JSON. you can understand "that ARRAY" in your JSON as been a "table", then if exists an array into another array, is as a Master-Detail relationship. you see? in your REST Debugger you can determinate "what the root", or be "where" you desire get the data on JSON result... look at on results tab... later this, just copy/paste the components for your project... JSON root Element = RESULTs[ 0 ] Results[ 0 ].TableABC https://docwiki.embarcadero.com/RADStudio/Alexandria/en/REST_Debugger_Tool what it would be "get info"? Edited June 12, 2023 by programmerdelphi2k Share this post Link to post
programmerdelphi2k 237 Posted June 12, 2023 (edited) here a sample to convert a JSON to Dataset uses REST.Response.Adapter, System.JSON; procedure JsonToDataset(aDataset : TDataSet; aJSON : string); var JObj: TJSONArray; vConv : TCustomJSONDataSetAdapter; begin if (aJSON = EmptyStr) then begin Exit; end; JObj := TJSONObject.ParseJSONValue(aJSON) as TJSONArray; vConv := TCustomJSONDataSetAdapter.Create(Nil); try vConv.Dataset := aDataset; vConv.UpdateDataSet(JObj); finally vConv.Free; JObj.Free; end; end; Quote JsonToDataset(FDMemTable1, My_JSON); Edited June 12, 2023 by programmerdelphi2k 1 Share this post Link to post
grantful 3 Posted June 12, 2023 @programmerdelphi2k Thanks for your patience and help. I will study this. Share this post Link to post