JSONStreamingBuilder - 범위 지정됨

  • 릴리스 버전: Washingtondc
  • 업데이트 날짜 2024년 02월 01일
  • 읽기18분
  • 외부 공급업체 API에 대량 데이터를 전송하기 위해 REST 또는 SOAP 요청에서 사용할 대규모 스트리밍 JSON 페이로드를 빌드하는 데 사용되는 빌더 객체를 생성합니다. 페이로드를 비스트리밍 옵션에 대한 JSON 문자열로 생성할 수도 있습니다.

    sn_ih 네임스페이스 식별자와 함께 스크립트 단계에서 다음 메서드를 플로우 디자이너 사용합니다. 예를 들어, 이 API를 사용하여 스크립트 단계에서 JSON 페이로드를 플로우 디자이너 생성하고 반환된 값을 REST 단계에 전달하여 외부 공급업체 서비스에 요청을 보낼 수 있습니다. 자세한 내용은 플로우 디자이너 스크립트 단계를 참조하십시오.

    이 API는 플로우 디자이너 환경 내에서만 사용할 수 있습니다.

    API 호출 순서

    다음 순서로 이러한 API를 사용하여 JSON 페이로드를 생성합니다.

    JSONStreamingBuilder: 빌더 오브젝트 작성
    다음 순서대로 이러한 메서드를 사용하여 작성기 개체를 만듭니다.
    1. JSONStreamingBuilder():JSONStreamingBuilder 오브젝트를 인스턴스화합니다.
    2. withAttachment(): 선택 사항입니다. JSON 객체를 스트리밍 첨부 파일로 생성하고 스트리밍 첨부 파일 [streaming_attachment] 테이블에 저장합니다. 이 메서드를 호출하지 않으면 API는 페이로드를 JSON 문자열로 생성합니다.
    3. expiresAt(): 선택 사항입니다. 첨부 파일이 만료되는 시간을 설정합니다. withAttachment() 메서드도 호출해야 합니다.
    4. build():JSONStreamingAPI 오브젝트를 리턴합니다.
    JSONStreamingAPI: JSON 페이로드를 빌드합니다.
    다음 순서에 따라 이 메서드를 사용하여 JSON 페이로드를 생성합니다.
    1. startObject(): 상위 JSON 객체를 만듭니다.
    2. writeFieldName(),writeString(),writeNumberField()와 같은 JSON 키-값 쌍을 생성하는 메서드입니다.
    3. endObject():상위 JSON 객체를 닫습니다.
    4. getJSONString() 또는 getAttachmentId(): 사용자가 작성한 JSON 문자열 또는 첨부 파일 ID를 리턴합니다.
    5. close(): JSONStreamingAPI 오브젝트를 닫습니다.

    크기 제한

    이 API를 통해 생성된 페이로드는 다음 크기 제한을 초과할 수 없습니다.

    • 첨부 파일: 200MB
    • 문자열: 5MB

    예제

    이 예제에서는 JSON 객체를 생성하여 정의된 만료 날짜가 있는 Attachment [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 - 빌드()

    JSONStreamingAPI 오브젝트를 리턴합니다.

    표 1. 매개변수
    이름 유형 설명
    없음
    표 2. 반환
    유형 설명
    JSONStreamingAPI 페이로드를 생성하는 데 사용되는 스트리밍 JSON 객체입니다.

    이 예제에서는 JSON 객체를 생성하여 정의된 만료 날짜가 있는 Attachment [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시간 후에 첨부 파일이 만료됩니다.

    표 3. 매개변수
    이름 유형 설명
    만료 GlideDateTime 첨부 파일이 만료될 때 설정되는 객체입니다.
    • 최소값: 첨부 파일이 생성된 시점부터 7200초 또는 2시간입니다. 기본.
    • 최댓값: 첨부 파일을 생성한 시점부터 172,800초 또는 48시간
    표 4. 반환
    유형 설명
    JSONStreaming빌더 JSON 페이로드를 시작하는 데 사용되는 작성기 객체입니다.

    이 예제에서는 JSON 객체를 생성하여 정의된 만료 날짜가 있는 Attachment [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 - JSONStreamingBuilder()

    JSONStreamingBuilder 오브젝트를 인스턴스화합니다.

    표 5. 매개변수
    이름 유형 설명
    없음
    var builder = new sn_ih.JSONStreamingBuilder()

    JSONStreamingBuilder - withAttachment()

    JSON 객체를 스트리밍 첨부 파일로 생성하고 스트리밍 첨부 파일 [streaming_attachment] 테이블에 저장합니다. 이 메서드를 호출하지 않으면 API는 페이로드를 JSON 문자열로 생성합니다.

    표 6. 매개변수
    이름 유형 설명
    없음
    표 7. 반환
    유형 설명
    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"
    } ]
    }