JSONStreamingBuilder - 범위 지정됨
REST 또는 SOAP 요청에서 외부 공급업체 API로 대량 데이터를 보내는 데 사용할 대규모 스트리밍 JSON 페이로드를 빌드하는 데 사용되는 빌더 객체를 생성합니다. 비스트리밍 옵션에 대한 JSON 문자열로 페이로드를 생성할 수도 있습니다.
스크립트 단계에서 sn_ih 네임스페이스 식별자와 함께 이러한 메서드를 워크플로우 스튜디오 사용합니다. 예를 들어 이 API를 사용하여 스크립트 단계에서 JSON 페이로드 워크플로우 스튜디오 를 생성하고 반환된 값을 REST 단계에 전달하여 요청을 외부 공급업체 서비스로 보낼 수 있습니다. 추가 정보를 보려면 워크플로우 스튜디오 스크립트 단계를 참조하십시오.
환경 내에서만 이 API를 사용할 수 있습니다 워크플로우 스튜디오 .
API 호출 순서
다음 순서로 이러한 API를 사용하여 JSON 페이로드를 생성합니다.
- JSONStreamingBuilder: 빌더 객체를 만듭니다.
- 다음 순서로 이러한 메서드를 사용하여 빌더 개체를 만듭니다.
- JSONStreamingBuilder():JSONStreamingBuilder 객체를 인스턴스화합니다.
- withAttachment(): 선택 사항입니다. JSON 객체를 스트리밍 첨부 파일로 만들고 스트리밍 첨부 파일 [streaming_attachment] 테이블에 저장합니다. 이 메서드를 호출하지 않으면 API는 페이로드를 JSON 문자열로 생성합니다.
- expiresAt(): 선택 사항입니다. 첨부 파일이 만료되는 시간을 설정합니다. 또한 withAttachment() 메서드를 호출해야 합니다.
- build(): JSONStreamingAPI 객체를 반환합니다.
- JSONStreamingAPI: JSON 페이로드를 빌드합니다.
- 다음 순서로 이러한 메서드를 사용하여 JSON 페이로드를 생성합니다.
- startObject():상위 JSON 객체를 만듭니다.
- writeFieldName(), writeString() 및 writeNumberField()와 같은 JSON 키-값 쌍을 생성하는 메서드입니다.
- endObject():상위 JSON 객체를 닫습니다.
- getJSONString() 또는 getAttachmentId(): 생성한 JSON 문자열 또는 첨부 파일 ID를 반환합니다.
- close():JSONStreamingAPI 객체를 닫습니다.
크기 제한
이 API를 통해 생성된 페이로드는 다음 크기 제한을 초과할 수 없습니다.
- 첨부 파일: 200MB
- 문자열: 5MB
예제
이 예시에서는 JSON 객체를 생성한 후 정의된 만료 날짜가 있는 첨부 파일 [sys_attachment] 테이블에 저장하는 방법을 보여줍니다.
try {
var ttl = new GlideDateTime("2011-01-01 12:00:00");
var builder = new sn_ih.JSONStreamingBuilder()
.withAttachment() // Creates the JSON object in streaming mode within an attachment.
.expiresAt(ttl) // Sets an expiration date for the attachment.
.build(); // Creates the JSONStreamingAPI object.
builder.startObject() // Begins generating the JSON object.
.writeFieldName("firstName") // Adds a "firstName" field
.writeString("John") // Writes the value of the "firstName" field
.writeFieldName("lastName")
.writeString("Smith")
.writeNumberField("age","25") // Write a number field named "age" with value "25"
.writeFieldName("address")
.startObject() // Start a new object nested under the parent object
.writeStringField("streetAddress", "21 2nd Street")
.writeStringField("city", "Santa Clara")
.writeStringField("state", "CA")
.writeStringField("postalCode", "11111")
.endObject()
.writeFieldName("phoneNumber")
.startArray() // Start an array
.startObject() // Add the first object to the array
.writeFieldName("type")
.writeString("home")
.writeFieldName("number")
.writeString("212 555-1234")
.endObject()
.startObject() // Add another object to the array
.writeFieldName("type")
.writeString("fax")
.writeFieldName("number")
.writeString("646 555-4567")
.endObject()
.endArray()
.endObject()
gs.log(builder.getAttachmentId()); // Returns the sys_id of the attachment.
}
catch (err) {
gs.log(err);
}
finally {
builder.close();
}또는 이 예시에서는 스크립트 단계에서 API를 사용하고 페이로드를 JSON 문자열로 생성하는 방법을 보여줍니다. 이 옵션을 사용하여 5MB 미만의 페이로드를 생성할 수 있습니다.
(function execute(inputs, outputs) {
var builder = new sn_ih.JSONStreamingBuilder().build();
builder.startObject()
.enablePrettyPrint()
.writeTextElement("firstName","John")
.writeString("John")
.writeFieldName("lastName")
.writeString("Smith")
.writeNumberField("age","25")
.writeFieldName("address")
.startObject()
.writeStringField("streetAddress", "21 2nd Street")
.writeStringField("city", "Santa Clara")
.writeStringField("state", "CA")
.writeStringField("postalCode", "11111")
.endObject()
.writeFieldName("phoneNumber")
.startArray()
.startObject()
.writeFieldName("type")
.writeString("home")
.writeFieldName("number")
.writeString("212 555-1234")
.endObject()
.startObject()
.writeFieldName("type")
.writeString("fax")
.writeFieldName("number")
.writeString("646 555-4567")
.endObject()
.endArray()
.endObject()
outputs.payload = builder.getJSONString();
})(inputs, outputs);출력:
{
"firstName" : "John",
"lastName" : "Smith",
"age" : 25,
"address" : {
"streetAddress" : "21 2nd Street",
"city" : "Santa Clara",
"state" : "CA",
"postalCode" : "11111"
},
"phoneNumber" : [ {
"type" : "home",
"number" : "212 555-1234"
}, {
"type" : "fax",
"number" : "646 555-4567"
} ]
}JSONStreamingBuilder - JSONStreamingBuilder()
JSONStreamingBuilder 객체를 인스턴스화합니다.
| 이름 | 유형 | 설명 |
|---|---|---|
| 안 함 |
var builder = new sn_ih.JSONStreamingBuilder()
JSONStreamingBuilder - build()
JSONStreamingAPI 객체를 반환합니다.
| 이름 | 유형 | 설명 |
|---|---|---|
| 없음 |
| 유형 | 설명 |
|---|---|
| JSONStreamingAPI | 페이로드를 구성하는 데 사용되는 스트리밍 JSON 객체입니다. |
이 예시에서는 JSON 객체를 생성한 후 정의된 만료 날짜가 있는 첨부 파일 [sys_attachment] 테이블에 저장하는 방법을 보여줍니다.
try {
var ttl = new GlideDateTime("2011-01-01 12:00:00");
var builder = new sn_ih.JSONStreamingBuilder()
.withAttachment() // Creates the JSON object in streaming mode within an attachment.
.expiresAt(ttl) // Sets an expiration date for the attachment.
.build(); // Creates the JSONStreamingAPI object.
builder.startObject() // Begins generating the JSON object.
.writeFieldName("firstName") // Adds a "firstName" field
.writeString("John") // Writes the value of the "firstName" field
.writeFieldName("lastName")
.writeString("Smith")
.writeNumberField("age","25") // Write a number field named "age" with value "25"
.writeFieldName("address")
.startObject() // Start a new object nested under the parent object
.writeStringField("streetAddress", "21 2nd Street")
.writeStringField("city", "Santa Clara")
.writeStringField("state", "CA")
.writeStringField("postalCode", "11111")
.endObject()
.writeFieldName("phoneNumber")
.startArray() // Start an array
.startObject() // Add the first object to the array
.writeFieldName("type")
.writeString("home")
.writeFieldName("number")
.writeString("212 555-1234")
.endObject()
.startObject() // Add another object to the array
.writeFieldName("type")
.writeString("fax")
.writeFieldName("number")
.writeString("646 555-4567")
.endObject()
.endArray()
.endObject()
gs.log(builder.getAttachmentId()); // Returns the sys_id of the attachment.
}
catch (err) {
gs.log(err);
}
finally {
builder.close();
}
JSONStreamingBuilder - expiresAt(객체 expiresAt)
첨부 파일이 만료되는 시간을 설정합니다. 또한 withAttachment() 메서드를 호출해야 합니다. 이 메서드를 호출하지 않으면 첨부 파일이 생성된 시간으로부터 2시간 후에 첨부 파일이 만료됩니다.
| 이름 | 유형 | 설명 |
|---|---|---|
| expiresAt | Glide날짜/시간 | 첨부 파일이 만료될 때 설정되는 객체입니다.
|
| 유형 | 설명 |
|---|---|
| JSONStreaming빌더 | JSON 페이로드를 시작하는 데 사용되는 빌더 객체입니다. |
이 예시에서는 JSON 객체를 생성한 후 정의된 만료 날짜가 있는 첨부 파일 [sys_attachment] 테이블에 저장하는 방법을 보여줍니다.
try {
var ttl = new GlideDateTime("2011-01-01 12:00:00");
var builder = new sn_ih.JSONStreamingBuilder()
.withAttachment() // Creates the JSON object in streaming mode within an attachment.
.expiresAt(ttl) // Sets an expiration date for the attachment.
.build(); // Creates the JSONStreamingAPI object.
builder.startObject() // Begins generating the JSON object.
.writeFieldName("firstName") // Adds a "firstName" field
.writeString("John") // Writes the value of the "firstName" field
.writeFieldName("lastName")
.writeString("Smith")
.writeNumberField("age","25") // Write a number field named "age" with value "25"
.writeFieldName("address")
.startObject() // Start a new object nested under the parent object
.writeStringField("streetAddress", "21 2nd Street")
.writeStringField("city", "Santa Clara")
.writeStringField("state", "CA")
.writeStringField("postalCode", "11111")
.endObject()
.writeFieldName("phoneNumber")
.startArray() // Start an array
.startObject() // Add the first object to the array
.writeFieldName("type")
.writeString("home")
.writeFieldName("number")
.writeString("212 555-1234")
.endObject()
.startObject() // Add another object to the array
.writeFieldName("type")
.writeString("fax")
.writeFieldName("number")
.writeString("646 555-4567")
.endObject()
.endArray()
.endObject()
gs.log(builder.getAttachmentId()); // Returns the sys_id of the attachment.
}
catch (err) {
gs.log(err);
}
finally {
builder.close();
}
JSONStreamingBuilder - 첨부 파일 () 포함
JSON 객체를 스트리밍 첨부 파일로 만들고 스트리밍 첨부 파일 [streaming_attachment] 테이블에 저장합니다. 이 메서드를 호출하지 않으면 API는 페이로드를 JSON 문자열로 생성합니다.
| 이름 | 유형 | 설명 |
|---|---|---|
| 없음 |
| 유형 | 설명 |
|---|---|
| JSONStreaming빌더 | JSON 페이로드를 시작하는 데 사용되는 빌더 객체입니다. |
이 예시에서는 JSON 페이로드를 빌드하고 이를 첨부 파일로 저장하는 방법을 보여줍니다.
try {
var ttl = new GlideDateTime("2011-01-01 12:00:00");
var builder = new sn_ih.JSONStreamingBuilder()
.withAttachment() // Creates the JSON object in streaming mode within an attachment.
.expiresAt(ttl) // Sets an expiration date for the attachment.
.build(); // Creates the JSONStreamingAPI object.
builder.startObject() // Begins generating the JSON object.
.writeFieldName("firstName") // Adds a "firstName" field
.writeString("John") // Writes the value of the "firstName" field
.writeFieldName("lastName")
.writeString("Smith")
.writeNumberField("age","25") // Write a number field named "age" with value "25"
.writeFieldName("address")
.startObject() // Start a new object nested under the parent object
.writeStringField("streetAddress", "21 2nd Street")
.writeStringField("city", "Santa Clara")
.writeStringField("state", "CA")
.writeStringField("postalCode", "11111")
.endObject()
.writeFieldName("phoneNumber")
.startArray() // Start an array
.startObject() // Add the first object to the array
.writeFieldName("type")
.writeString("home")
.writeFieldName("number")
.writeString("212 555-1234")
.endObject()
.startObject() // Add another object to the array
.writeFieldName("type")
.writeString("fax")
.writeFieldName("number")
.writeString("646 555-4567")
.endObject()
.endArray()
.endObject()
gs.log(builder.getAttachmentId()); // Returns the sys_id of the attachment.
}
catch (err) {
gs.log(err);
}
finally {
builder.close();
}
이 예시에서는 JSON 페이로드를 빌드하고 문자열로 저장하는 방법을 보여줍니다.
try {
var builder = new sn_ih.JSONStreamingBuilder().build();
builder.startObject()
.writeFieldName("firstName")
.writeString("John")
.writeFieldName("lastName")
.writeString("Smith")
.writeNumberField("age","25")
.writeFieldName("address")
.startObject()
.writeStringField("streetAddress", "21 2nd Street")
.writeStringField("city", "Santa Clara")
.writeStringField("state", "CA")
.writeStringField("postalCode", "11111")
.endObject()
.writeFieldName("phoneNumber")
.startArray()
.startObject()
.writeFieldName("type")
.writeString("home")
.writeFieldName("number")
.writeString("212 555-1234")
.endObject()
.startObject()
.writeFieldName("type")
.writeString("fax")
.writeFieldName("number")
.writeString("646 555-4567")
.endObject()
.endArray()
.endObject()
gs.log(builder.getJSONString());
}
catch (err) {
gs.log("Exception: " + err);
}
finally {
builder.close();
}
{
"firstName" : "John",
"lastName" : "Smith",
"age" : 25,
"address" : {
"streetAddress" : "21 2nd Street",
"city" : "Santa Clara",
"state" : "CA",
"postalCode" : "11111"
},
"phoneNumber" : [ {
"type" : "home",
"number" : "212 555-1234"
}, {
"type" : "fax",
"number" : "646 555-4567"
} ]
}