Created
January 8, 2025 03:16
Read a JSON stream from a REST service through Apache HttpClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var result = (function () { | |
// HC Client Types | |
var HttpClients = Packages.org.apache.http.impl.client.HttpClients; | |
var HttpGet = Packages.org.apache.http.client.methods.HttpGet | |
var JsonClass = Packages.jakarta.json.Json; | |
var START_OBJECT = Packages.jakarta.json.stream.JsonParser.Event.START_OBJECT; | |
// PeopleCode Types | |
var CreateSQL = Packages.PeopleSoft.PeopleCode.Func.CreateSQL; | |
var values = Java.to([null, null, null, null, null],"java.lang.Object[]"); | |
var results = []; | |
var SQL = CreateSQL("INSERT INTO PS_JSM_PHOTOS(JSM_ALBUM_ID, JSM_ID, DESCR254, URL, IMAGE_URL) VALUES (:1, :2, :3, :4, :5)"); | |
// Mainline | |
var hcClient = HttpClients.createDefault(); | |
var request = new HttpGet("https://jsonplaceholder.typicode.com/photos"); | |
var response = hcClient.execute(request); | |
var parser = JsonClass.createParser(response.getEntity().getContent()); | |
var count = 0; | |
while (parser.hasNext()) { | |
var event = parser.next(); | |
if (event == START_OBJECT) { | |
var photo = parser.getObject(); | |
// save photo details to database | |
values[0] = photo.getInt("albumId"); | |
values[1] = photo.getInt("id"); | |
values[2] = photo.getString("title"); | |
values[3] = photo.getString("url"); | |
values[4] = photo.getString("thumbnailUrl"); | |
SQL.Execute(values); | |
count++; | |
} | |
} | |
// Return the response to PeopleCode. | |
return count; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment