นโยบาย XMLtoJSON

คุณกำลังดูเอกสารประกอบของ Apigee Edge
ไปที่เอกสารประกอบของ Apigee X
info

อะไร

นโยบายนี้จะแปลงข้อความจากรูปแบบ Extensible Markup Language (XML) เป็น JavaScript Object Notation (JSON) ซึ่งจะช่วยให้คุณมีตัวเลือกหลายอย่างในการควบคุมวิธีแปลงข้อความ

หากต้องการแปลงการตอบกลับในรูปแบบ XML เป็นการตอบกลับในรูปแบบ JSON นโยบายจะแนบไปกับโฟลว์การตอบกลับ (เช่น Response / ProxyEndpoint / PostFlow)

เกี่ยวกับ

ในสถานการณ์การไกล่เกลี่ยทั่วไป นโยบาย JSON เป็น XML ในโฟลว์คำขอขาเข้ามักจะ จับคู่กับนโยบาย XML เป็น JSON ในโฟลว์การตอบกลับขาออก การรวมนโยบายด้วยวิธีนี้จะช่วยให้แสดง JSON API สำหรับบริการแบ็กเอนด์ที่รองรับเฉพาะ XML ได้

สำหรับสถานการณ์ที่แอปไคลเอ็นต์ต่างๆ ใช้ API ซึ่งอาจต้องใช้ JSON หรือ XML คุณสามารถตั้งค่ารูปแบบการตอบกลับแบบไดนามิกได้โดยการกำหนดค่านโยบาย JSON เป็น XML และ XML เป็น JSON เพื่อดำเนินการแบบมีเงื่อนไข ดูการติดตั้งใช้งานสถานการณ์นี้ได้ที่ตัวแปรและเงื่อนไขของโฟลว์


ตัวอย่าง

ดูรายละเอียดเกี่ยวกับการแปลงระหว่าง JSON กับ XML ได้ที่การแปลงระหว่าง XML กับ JSON ด้วย Apigee: สิ่งที่คุณต้องรู้

การแปลงคำตอบ

<XMLToJSON name="ConvertToJSON">
  <Options>
  </Options>
  <OutputVariable>response</OutputVariable>
  <Source>response</Source>
</XMLToJSON>

การกำหนดค่านี้ซึ่งเป็นการกำหนดค่าขั้นต่ำที่จำเป็นในการแปลง XML เป็น JSON จะใช้ข้อความตอบกลับในรูปแบบ XML เป็นแหล่งที่มา จากนั้นจะสร้างข้อความในรูปแบบ JSON ที่ป้อนใน response OutputVariable Edge จะใช้เนื้อหาของตัวแปรนี้เป็นข้อความสำหรับขั้นตอนการประมวลผลถัดไปโดยอัตโนมัติ


การอ้างอิงองค์ประกอบ

ต่อไปนี้คือองค์ประกอบและแอตทริบิวต์ที่คุณกำหนดค่าในนโยบายนี้ได้

<XMLToJSON async="false" continueOnError="false" enabled="true" name="XML-to-JSON-1">
    <DisplayName>XML to JSON 1</DisplayName>
    <Source>response</Source>
    <OutputVariable>response</OutputVariable>
    <Options>
        <RecognizeNumber>true</RecognizeNumber>
        <RecognizeBoolean>true</RecognizeBoolean>
        <RecognizeNull>true</RecognizeNull>
        <NullValue>NULL</NullValue>
        <NamespaceBlockName>#namespaces</NamespaceBlockName>
        <DefaultNamespaceNodeName>&</DefaultNamespaceNodeName>
        <NamespaceSeparator>***</NamespaceSeparator>
        <TextAlwaysAsProperty>true</TextAlwaysAsProperty>
        <TextNodeName>TEXT</TextNodeName>
        <AttributeBlockName>FOO_BLOCK</AttributeBlockName>
        <AttributePrefix>BAR_</AttributePrefix>
        <OutputPrefix>PREFIX_</OutputPrefix>
        <OutputSuffix>_SUFFIX</OutputSuffix>
        <StripLevels>2</StripLevels>
        <TreatAsArray>
            <Path unwrap="true">teachers/teacher/studentnames/name</Path>
        </TreatAsArray>
    </Options>
    <!-- Use Options or Format, not both -->
    <Format>yahoo</Format>
</XMLToJSON>

แอตทริบิวต์ <XMLtoJSON>

<XMLtoJSON async="false" continueOnError="false" enabled="true" name="XML-to-JSON-1">

The following table describes attributes that are common to all policy parent elements:

Attribute Description Default Presence
name

The internal name of the policy. The value of the name attribute can contain letters, numbers, spaces, hyphens, underscores, and periods. This value cannot exceed 255 characters.

Optionally, use the <DisplayName> element to label the policy in the management UI proxy editor with a different, natural-language name.

N/A Required
continueOnError

Set to false to return an error when a policy fails. This is expected behavior for most policies.

Set to true to have flow execution continue even after a policy fails.

false Optional
enabled

Set to true to enforce the policy.

Set to false to turn off the policy. The policy will not be enforced even if it remains attached to a flow.

true Optional
async

This attribute is deprecated.

false Deprecated

<DisplayName> element

Use in addition to the name attribute to label the policy in the management UI proxy editor with a different, natural-language name.

<DisplayName>Policy Display Name</DisplayName>
Default

N/A

If you omit this element, the value of the policy's name attribute is used.

Presence Optional
Type String

องค์ประกอบ <Source>

ตัวแปร คำขอ หรือการตอบกลับที่มีข้อความ XML ที่คุณต้องการแปลงเป็น JSON

ต้องตั้งค่าส่วนหัวประเภทเนื้อหา HTTP ของข้อความต้นทางเป็น application/xml มิเช่นนั้นนโยบายจะไม่มีผล

หากไม่ได้กำหนด <Source> ไว้ ระบบจะถือว่าเป็นข้อความ (ซึ่งจะเปลี่ยนเป็นคำขอเมื่อแนบนโยบายกับโฟลว์คำขอ หรือเปลี่ยนเป็นการตอบกลับเมื่อแนบนโยบายกับโฟลว์การตอบกลับ)

หากไม่สามารถระบุตัวแปรแหล่งที่มา หรือระบุเป็นประเภทที่ไม่ใช่ข้อความ นโยบาย จะแสดงข้อผิดพลาด

<Source>response</Source>
ค่าเริ่มต้น คำขอหรือการตอบกลับ ซึ่งกำหนดโดยตำแหน่งที่เพิ่มนโยบายลงในโฟลว์พร็อกซี API
การตรวจหาบุคคลในบ้าน ไม่บังคับ
ประเภท ข้อความ

องค์ประกอบ <OutputVariable>

จัดเก็บเอาต์พุตของการแปลง XML เป็นรูปแบบ JSON โดยปกติแล้วค่านี้จะเหมือนกับค่าของแหล่งที่มา นั่นคือโดยปกติแล้วการตอบกลับ XML จะได้รับการแปลงเป็นการตอบกลับ JSON

ระบบจะแยกวิเคราะห์เพย์โหลดของข้อความ XML และแปลงเป็น JSON จากนั้นตั้งค่าส่วนหัว Content-type ของ HTTP ของข้อความรูปแบบ XML เป็น application/json

หากไม่ได้ระบุ OutputVariable ระบบจะถือว่า source เป็น OutputVariable เช่น หาก source คือ response OutputVariable จะมีค่าเริ่มต้นเป็น response

<OutputVariable>response</OutputVariable>
ค่าเริ่มต้น คำขอหรือการตอบกลับ ซึ่งกำหนดโดยตำแหน่งที่เพิ่มนโยบายลงในโฟลว์พร็อกซี API
การตรวจหาบุคคลในบ้าน องค์ประกอบนี้จำเป็นเมื่อตัวแปรที่กำหนดในองค์ประกอบ <Source> เป็นประเภทสตริง
ประเภท ข้อความ

<ตัวเลือก>

ตัวเลือกช่วยให้คุณควบคุมการแปลงจาก XML เป็น JSON ได้ ใช้กลุ่ม <Options> ซึ่งช่วยให้คุณเพิ่มการตั้งค่า Conversion ที่เฉพาะเจาะจง หรือใช้องค์ประกอบ <Format> ซึ่งช่วยให้คุณอ้างอิงเทมเพลตของ ตัวเลือกที่กำหนดไว้ล่วงหน้า คุณไม่สามารถใช้ทั้ง <Options> และ <Format>

ต้องระบุ <Options> หากไม่ได้ใช้ <Format>

องค์ประกอบ <Options>/<RecognizeNumber>

หากเป็นจริง ฟิลด์ตัวเลขในเพย์โหลด XML จะยังคงรูปแบบเดิม

<RecognizeNumber>true</RecognizeNumber>

ลองดูตัวอย่าง XML ต่อไปนี้

<a>
  <b>100</b>
  <c>value</c>
</a>

หาก true จะแปลงเป็น

{
    "a": {
        "b": 100,
        "c": "value"
    }
}

หาก false จะแปลงเป็น

{
    "a": {
        "b": "100",
        "c": "value"
    }
}
ค่าเริ่มต้น เท็จ
การตรวจหาบุคคลในบ้าน ไม่บังคับ
ประเภท บูลีน

องค์ประกอบ <Options>/<RecognizeBoolean>

ช่วยให้ Conversion รักษาค่าบูลีนเป็นจริง/เท็จไว้ได้แทนที่จะเปลี่ยนค่าเป็นสตริง

<RecognizeBoolean>true</RecognizeBoolean>

สำหรับตัวอย่าง XML ต่อไปนี้

<a>
  <b>true</b>
  <c>value</c>
</a>

หาก true จะแปลงเป็น

{
    "a": {
        "b": true,
        "c": "value"
    }
}

หาก false จะแปลงเป็น

{
    "a": {
        "b": "true",
        "c": "value"
    }
}
ค่าเริ่มต้น เท็จ
การตรวจหาบุคคลในบ้าน ไม่บังคับ
ประเภท บูลีน

องค์ประกอบ <Options>/<RecognizeNull>

ช่วยให้คุณแปลงค่าว่างเป็นค่า Null ได้

<RecognizeNull>true</RecognizeNull>

สำหรับ XML ต่อไปนี้

<a>
  <b></b>
  <c>value</c>
</a>

หาก true จะแปลงเป็น

{
  "a": {
    "b": null,
    "c": "value"
  }
}

หาก false จะแปลงเป็น

{
  "a": {
    "b": {},
    "c": "value"
  }
}
ค่าเริ่มต้น เท็จ
การตรวจหาบุคคลในบ้าน ไม่บังคับ
ประเภท บูลีน

องค์ประกอบ <Options>/<NullValue>

ระบุค่าที่ควรแปลงค่า Null ที่ระบบรู้จักในข้อความต้นฉบับ โดยค่าเริ่มต้น ค่าจะเป็น null ตัวเลือกนี้จะมีผลก็ต่อเมื่อ RecognizeNull เป็นจริงเท่านั้น

<NullValue>not-present</NullValue>

ค่าเริ่มต้น null
การตรวจหาบุคคลในบ้าน ไม่บังคับ
ประเภท สตริง

องค์ประกอบ <Options>/<NamespaceBlockName>
<Options>/<DefaultNamespaceNodeName>
<Options>/<NamespaceSeparator>

ใช้องค์ประกอบเหล่านี้ร่วมกัน

<NamespaceBlockName>#namespaces</NamespaceBlockName>
<DefaultNamespaceNodeName>&</DefaultNamespaceNodeName>
<NamespaceSeparator>***</NamespaceSeparator>

ลองดูตัวอย่าง XML ต่อไปนี้

<a xmlns="http://ns.com" xmlns:ns1="http://ns1.com">
  <ns1:b>value</ns1:b>
</a>

หากไม่ได้ระบุ NamespaceSeparator ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้

{
    "a": {
        "b": "value"
    }
}

หากระบุองค์ประกอบ NamespaceBlockName, DefaultNamespaceNodeName และ NamespaceSeparator เป็น #namespaces, & และ *** ตามลำดับ ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้

{
    "a": {
        "#namespaces": {
            "&": "http://ns.com",
            "ns1": "http://ns1.com"
        },
        "ns1***b": "value"
    }
}
ค่าเริ่มต้น ดูตัวอย่างด้านบน
การตรวจหาบุคคลในบ้าน ไม่บังคับ
อย่างไรก็ตาม หากระบุ <NamespaceBlockName> คุณต้องระบุ อีก 2 องค์ประกอบด้วย
ประเภท สตริง

องค์ประกอบ <Options>/<TextAlwaysAsProperty>
<Options>/<TextNodeName>

ใช้องค์ประกอบเหล่านี้ร่วมกัน

หากตั้งค่าเป็น true ระบบจะแปลงเนื้อหาขององค์ประกอบ XML เป็นพร็อพเพอร์ตี้สตริง

<TextAlwaysAsProperty>true</TextAlwaysAsProperty>
<TextNodeName>TEXT</TextNodeName>

สำหรับ XML ต่อไปนี้

<a>
  <b>value1</b>
  <c>value2<d>value3</d>value4</c>
</a>

หากตั้งค่า TextAlwaysAsProperty เป็น true และระบุ TextNodeName เป็น TEXT ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้

{
  "a": {
    "b": {
      "TEXT": "value1"
    },
    "c": {
      "TEXT": [
        "value2",
        "value4"
        ],
        "d": {
          "TEXT": "value3"
        }
      }
    }
}

หากตั้งค่า TextAlwaysAsProperty เป็น false และ ระบุ TextNodeName เป็น TEXT ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้

{
  "a": {
    "b": "value1",
    "c": {
      "TEXT": [
        "value2",
        "value4"
      ],
      {
        "d": "value3",
      }
    }
}
ค่าเริ่มต้น <TextAlwaysAsProperty>: เท็จ
<TextNodeName>: ไม่มี
การตรวจหาบุคคลในบ้าน ไม่บังคับ
ประเภท <TextAlwaysAsProperty>: บูลีน
<TextNodeName>: สตริง

<Options>/<AttributeBlockName>
องค์ประกอบ <Options>/<AttributePrefix>

ใช้องค์ประกอบเหล่านี้ร่วมกัน

ช่วยให้คุณจัดกลุ่มค่าเป็นบล็อก JSON และต่อท้ายคำนำหน้ากับชื่อแอตทริบิวต์

<AttributeBlockName>FOO_BLOCK</AttributeBlockName>
<AttributePrefix>BAR_</AttributePrefix>

ลองดูตัวอย่าง XML ต่อไปนี้

<a attrib1="value1" attrib2="value2"/>

หากระบุทั้งแอตทริบิวต์ (AttributeBlockName และ AttributePrefix) ตามที่กำหนดไว้ในตัวอย่าง XML เป็น JSON ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้

{
  "a": {
    "FOO_BLOCK": {
      "BAR_attrib1": "value1",
      "BAR_attrib2": "value2"
    }
  }
}

หากระบุเฉพาะ AttributeBlockName ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้ ขึ้น

{
    "a": {
        "FOO_BLOCK": {
            "attrib1": "value1",
            "attrib2": "value2"
        }
    }
}

หากระบุเฉพาะ AttributePrefix ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้ ขึ้น

{
    "a": {
        "BAR_attrib1": "value1",
        "BAR_attrib2": "value2"
    }
}

หากไม่ได้ระบุทั้ง 2 อย่าง ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้

{
    "a": {
        "attrib1": "value1",
        "attrib2": "value2"
    }
}
ค่าเริ่มต้น ดูตัวอย่างด้านบน
การตรวจหาบุคคลในบ้าน ไม่บังคับ
ประเภท สตริง

องค์ประกอบ <Options>/<OutputPrefix>
<Options>/<OutputSuffix>

ใช้องค์ประกอบเหล่านี้ร่วมกัน

<OutputPrefix>PREFIX_</OutputPrefix>
<OutputSuffix>_SUFFIX</OutputSuffix>

ลองดูตัวอย่าง XML ต่อไปนี้

<a>value</a>

หากระบุทั้งแอตทริบิวต์ (OutputPrefix และ OutputSuffix) ตามที่กำหนดไว้ในตัวอย่าง XML เป็น JSON ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้

PREFIX_{
    "a": "value"
}_SUFFIX

หากระบุเฉพาะ OutputPrefix ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้

PREFIX_{
  "a" : "value"
}

หากระบุเฉพาะ OutputSuffix ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้

{
  "a" : "value"
}_SUFFIX

หากไม่ได้ระบุทั้ง OutputPrefix และ OutputSuffix ระบบจะสร้างโครงสร้าง JSON ต่อไปนี้

{
    "a": "value"
}
ค่าเริ่มต้น ดูตัวอย่างด้านบน
การตรวจหาบุคคลในบ้าน ไม่บังคับ
ประเภท สตริง

องค์ประกอบ <Options>/<StripLevels>

<Options>
    <StripLevels>4</StripLevels>
</Options>

บางครั้งเพย์โหลด XML เช่น SOAP มีระดับพาเรนต์หลายระดับที่คุณไม่ต้องการรวมไว้ใน JSON ที่แปลงแล้ว ต่อไปนี้คือตัวอย่างการตอบกลับ SOAP ที่มีหลายระดับ

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/Schemata-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
      <GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/">
          <GetCityWeatherByZIPResult>
              <State>CO</State>
              <City>Denver</City>
              <Description>Sunny</Description>
              <Temperature>62</Temperature>
          </GetCityWeatherByZIPResult>
      </GetCityWeatherByZIPResponse>
  </soap:Body>
</soap:Envelope>

โดยมี 4 ระดับก่อนที่จะถึงระดับรัฐ เมือง คำอธิบาย และอุณหภูมิ หากไม่ใช้ <StripLevels> การตอบกลับ JSON ที่แปลงแล้วจะมีลักษณะดังนี้

{
   "Envelope" : {
      "Body" : {
         "GetCityWeatherByZIPResponse" : {
            "GetCityWeatherByZIPResult" : {
               "State" : "CO",
               "City" : "Denver",
               "Description" : "Sunny",
               "Temperature" : "62"
            }
         }
      }
   }
}

หากต้องการลบ 4 ระดับแรกในการตอบกลับ JSON คุณจะต้องตั้งค่า <StripLevels>4</StripLevels> ซึ่งจะให้ JSON ต่อไปนี้ แก่คุณ

{
  "State" : "CO",
  "City" : "Denver",
  "Description" : "Sunny",
  "Temperature" : "62"
}

คุณสามารถลบระดับออกได้จนถึงองค์ประกอบแรกที่มีองค์ประกอบย่อยหลายรายการ หมายความว่าอย่างไร มาดูตัวอย่าง JSON ที่ซับซ้อนขึ้นกัน

{
   "Envelope" : {
      "Body" : {
         "GetCityForecastByZIPResponse" : {
            "GetCityForecastByZIPResult" : {
               "ResponseText" : "City Found",
               "ForecastResult" : {
                  "Forecast" : [
                     {
                        "ProbabilityOfPrecipiation" : {
                           "Nighttime" : "00",
                           "Daytime" : 10
                        }  ...

ระดับ 3 ในตัวอย่างนี้คือ GetCityForecastByZIPResponse ซึ่งมีลูกเพียงคนเดียว ดังนั้นหากคุณใช้ <StripLevels>3</StripLevels> (นำ 3 ระดับแรกออก) JSON จะมีลักษณะดังนี้

{
   "GetCityForecastByZIPResult" : {
      "ResponseText" : "City Found",
      "ForecastResult" : {
         "Forecast" : [
            {
               "ProbabilityOfPrecipiation" : {
                  "Nighttime" : "00",
                  "Daytime" : 10
               }  ...

โปรดสังเกตว่า GetCityForecastByZIPResult มีองค์ประกอบย่อยหลายรายการ เนื่องจากเป็นองค์ประกอบแรกที่มีองค์ประกอบย่อยหลายรายการ คุณจึงลบระดับสุดท้ายนี้ได้โดยใช้ <StripLevels>4</StripLevels> ซึ่งจะให้ JSON ต่อไปนี้

{
   "ResponseText" : "City Found",
   "ForecastResult" : {
      "Forecast" : [
         {
            "ProbabilityOfPrecipiation" : {
               "Nighttime" : "00",
               "Daytime" : 10
            }  ...

เนื่องจากระดับ 4 เป็นระดับแรกที่มีโฟลเดอร์ย่อยหลายโฟลเดอร์ คุณจึงไม่สามารถแยกโฟลเดอร์ระดับที่ต่ำกว่านี้ได้ หากตั้งค่าระดับแถบเป็น 5, 6, 7 และอื่นๆ คุณจะยังคงได้รับคำตอบ ข้างต้น

ค่าเริ่มต้น 0 (ไม่มีการลบระดับ)
การตรวจหาบุคคลในบ้าน ไม่บังคับ
ประเภท จำนวนเต็ม

องค์ประกอบ <Options>/<TreatAsArray>/<Path>

<Options>
    <TreatAsArray>
        <Path unwrap="true">teachers/teacher/studentnames/name</Path>
    </TreatAsArray>
</Options>

การรวมองค์ประกอบนี้ช่วยให้มั่นใจได้ว่าค่าจากเอกสาร XML จะอยู่ในอาร์เรย์ JSON ซึ่งจะมีประโยชน์ เช่น เมื่อจำนวนองค์ประกอบย่อยอาจแตกต่างกัน (ตั้งแต่ 1 รายการขึ้นไป) และคุณต้องการให้ค่าอยู่ในอาร์เรย์เสมอ การทำเช่นนี้จะช่วยให้โค้ดของคุณเสถียร เนื่องจากคุณจะรับข้อมูลจากอาร์เรย์ได้ในลักษณะเดียวกันทุกครั้ง ตัวอย่างเช่น $.teachers.teacher.studentnames[0] จะรับค่าชื่อนักเรียนคนแรก ในอาร์เรย์โดยไม่คำนึงถึงจำนวนค่าในอาร์เรย์

มาดูพฤติกรรมเริ่มต้นของ XML เป็น JSON แล้วดูวิธี ควบคุมเอาต์พุตโดยใช้ <TreatAsArray>/<Path> กัน

เมื่อเอกสาร XML มีองค์ประกอบที่มีค่าลูกหลายค่า (โดยปกติจะอิงตามสคีมา ที่องค์ประกอบมี maxOccurs='unbounded') นโยบาย XML เป็น JSON จะ วางค่าเหล่านั้นไว้ในอาร์เรย์โดยอัตโนมัติ เช่น บล็อก XML ต่อไปนี้

<teacher>
    <name>teacherA</name>
    <studentnames>
        <name>student1</name>
        <name>student2</name>
    </studentnames>
</teacher>

...จะได้รับการแปลงเป็น JSON ต่อไปนี้โดยอัตโนมัติโดยไม่มีการกำหนดค่านโยบายพิเศษ

{
  "teachers" : {
      "teacher" : {
          "name" : "teacherA",
          "studentnames" : {
              "name" : [
                 "student1",
                 "student2"
              ]}
           }
      }
}

โปรดสังเกตว่าชื่อนักเรียน 2 คนจะอยู่ในอาร์เรย์

อย่างไรก็ตาม หากมีนักเรียนเพียงคนเดียวในเอกสาร XML นโยบาย XML เป็น JSON จะถือว่าค่าเป็นสตริงเดียวโดยอัตโนมัติ ไม่ใช่อาร์เรย์ของสตริง ดังที่แสดงในตัวอย่างต่อไปนี้

{
  "teachers" : {
      "teacher" : {
          "name" : "teacherA",
          "studentnames" : {
              "name" : "student1"
              }
          }
      }
}

ในตัวอย่างก่อนหน้า ระบบจะแปลงข้อมูลที่คล้ายกันในรูปแบบที่แตกต่างกัน โดยครั้งหนึ่งเป็นอาร์เรย์ และอีกครั้งเป็นสตริงเดียว องค์ประกอบ <TreatAsArray>/<Path> ช่วยให้คุณควบคุมเอาต์พุตได้ เช่น คุณสามารถตรวจสอบว่าระบบจะใส่ชื่อนักเรียนในอาร์เรย์เสมอแม้จะมีค่าเพียงค่าเดียวก็ตาม คุณกำหนดค่านี้ได้โดยระบุเส้นทางไปยังองค์ประกอบ ซึ่งมีค่าที่คุณต้องการใส่ในอาร์เรย์ ดังนี้

<Options>
    <TreatAsArray>
        <Path>teachers/teacher/studentnames/name</Path>
    </TreatAsArray>
</Options>

การกำหนดค่าข้างต้นจะเขียน JSON ดังนี้

{
  "teachers" : {
      "teacher" : {
          "name" : "teacherA",
          "studentnames" : {
              "name" : ["student1"]
              }
            ]
          }
      }
}

โปรดสังเกตว่าตอนนี้ student1 อยู่ในอาร์เรย์แล้ว ตอนนี้ไม่ว่าจะมีนักเรียน/นักศึกษาคนเดียวหรือหลายคน คุณก็สามารถดึงข้อมูลจากอาร์เรย์ JSON ในโค้ดได้โดยใช้ JSONPath ต่อไปนี้ $.teachers.teacher.studentnames.name[0]

นอกจากนี้ องค์ประกอบ <Path> ยังมีแอตทริบิวต์ unwrap ซึ่งอธิบายไว้ใน ส่วนถัดไป

ค่าเริ่มต้น NA
การตรวจหาบุคคลในบ้าน ไม่บังคับ
ประเภท สตริง

Attributes

 <Options>
    <TreatAsArray>
        <Path unwrap="true">teachers/teacher/studentnames/name</Path>
    </TreatAsArray>
</Options>
แอตทริบิวต์ คำอธิบาย การมีบุคคลอยู่ ประเภท
แยก

ค่าเริ่มต้น: เท็จ

นำองค์ประกอบออกจากเอาต์พุต JSON ใช้เพื่อเพิ่มประสิทธิภาพหรือลดความซับซ้อน ("คลาย") ของ JSON ซึ่งจะช่วยลดความยาวของ JSONPath ที่จำเป็นในการดึงค่าด้วย เช่น คุณสามารถแปลง JSON เป็นรูปแบบแบนและใช้ $.teachers.studentnames[*] แทน $.teachers.teacher.studentnames.name[*] ได้

ตัวอย่าง JSON มีดังนี้

{
  "teachers" : {
      "teacher" : {
          "name" : "teacherA",
          "studentnames" : {
              "name" : [
                 "student1",
                 "student2"
              ]}...

ในตัวอย่างนี้ คุณอาจโต้แย้งได้ว่าองค์ประกอบ teacher และองค์ประกอบ studentnames name ไม่จำเป็น คุณจึงนำออกหรือยกเลิกการห่อ ได้ คุณจะกำหนดค่าองค์ประกอบ <Path> เพื่อดำเนินการนี้ได้อย่างไร

<TreatAsArray>
    <Path unwrap="true">teachers/teacher</Path>
    <Path unwrap="true">teachers/teacher/studentnames/name</Path>
</TreatAsArray>

ตั้งค่าแอตทริบิวต์ unwrap เป็น true และระบุเส้นทางไปยังองค์ประกอบที่จะ แกะ ตอนนี้เอาต์พุต JSON จะมีลักษณะดังนี้

{
  "teachers" : [{
      "name" : "teacherA",
      "studentnames" : ["student1","student2"]
      }]...

โปรดทราบว่าเนื่องจากองค์ประกอบ <Path> อยู่ในองค์ประกอบ <TreatAsArray> องค์ประกอบทั้ง 2 ในเส้นทางจึงถือเป็นอาร์เรย์ในเอาต์พุต JSON

ไม่บังคับ บูลีน

ดูตัวอย่างเพิ่มเติมและคำแนะนำแบบทีละขั้นตอนเกี่ยวกับฟีเจอร์ได้ที่บทความในชุมชน Apigee นี้: บทแนะนำสำหรับชุมชน: ตัวเลือก TreatAsArray ในนโยบาย XML เป็น JSON

<Format>

รูปแบบช่วยให้คุณควบคุมการแปลงจาก XML เป็น JSON ได้ ป้อนชื่อเทมเพลตที่กำหนดไว้ล่วงหน้า ซึ่งมีชุดค่าผสมเฉพาะขององค์ประกอบตัวเลือกที่อธิบายไว้ในหัวข้อนี้ รูปแบบที่กำหนดไว้ล่วงหน้า ได้แก่ xml.com, yahoo, google, badgerFish

ใช้ทั้งองค์ประกอบ <Format> หรือกลุ่ม <Options> คุณไม่สามารถใช้ทั้ง <Format> และ <Options>

คำจำกัดความของรูปแบบของเทมเพลตที่กำหนดไว้ล่วงหน้าแต่ละรายการมีดังนี้

xml.com

<RecognizeNull>true</RecognizeNull>
<TextNodeName>#text</TextNodeName>
<AttributePrefix>@</AttributePrefix>

yahoo

<RecognizeNumber>true</RecognizeNumber>
<TextNodeName>content</TextNodeName>

Google

<TextNodeName>$t</TextNodeName>
<NamespaceSeparator>$</NamespaceSeparator>
<TextAlwaysAsProperty>true</TextAlwaysAsProperty>

badgerFish

<TextNodeName>$</TextNodeName>
<TextAlwaysAsProperty>true</TextAlwaysAsProperty>
<AttributePrefix>@</AttributePrefix>
<NamespaceSeparator>:</NamespaceSeparator>
<NamespaceBlockName>@xmlns</NamespaceBlockName>
<DefaultNamespaceNodeName>$</DefaultNamespaceNodeName>

ไวยากรณ์ขององค์ประกอบ

<Format>yahoo</Format>
ค่าเริ่มต้น ป้อนชื่อรูปแบบที่ใช้ได้
xml.com, yahoo, google, badgerFish
การตรวจหาบุคคลในบ้าน ต้องระบุหากไม่ได้ใช้ <Options>
ประเภท สตริง

สคีมา


ข้อมูลอ้างอิงข้อผิดพลาด

This section describes the fault codes and error messages that are returned and fault variables that are set by Edge when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.

Runtime errors

These errors can occur when the policy executes.

Fault code HTTP status Cause Fix
steps.xmltojson.ExecutionFailed 500 This error occurs when the input payload (XML) is empty or the input XML is invalid or malformed.
steps.xmltojson.InCompatibleType 500 This error occurs if the type of the variable defined in the <Source> element and the <OutputVariable> element are not the same. It is mandatory that the type of the variables contained within the <Source> element and the <OutputVariable> element matches.
steps.xmltojson.InvalidSourceType 500 This error occurs if the type of the variable used to define the <Source> element is invalid.The valid types of variable are message and string.
steps.xmltojson.OutputVariableIsNotAvailable 500 This error occurs if the variable specified in the <Source> element of the XML to JSON policy is of type string and the <OutputVariable> element is not defined. The <OutputVariable> element is mandatory when the variable defined in the <Source> element is of type string.
steps.xmltojson.SourceUnavailable 500 This error occurs if the message variable specified in the <Source> element of the XML to JSON policy is either:
  • out of scope (not available in the specific flow where the policy is being executed) or
  • can't be resolved (is not defined)

Deployment errors

These errors can occur when you deploy a proxy containing this policy.

Error name Cause Fix
EitherOptionOrFormat If one of the elements <Options> or <Format> is not declared in the XML to JSON Policy, then the deployment of the API proxy fails.
UnknownFormat If the <Format> element within the XML to JSON policy has an unknown format defined, then the deployment of the API proxy fails. Predefined formats include: xml.com, yahoo, google, and badgerFish.

Fault variables

These variables are set when a runtime error occurs. For more information, see What you need to know about policy errors.

Variables Where Example
fault.name="fault_name" fault_name is the name of the fault, as listed in the Runtime errors table above. The fault name is the last part of the fault code. fault.name = "SourceUnavailable"
xmltojson.policy_name.failed policy_name is the user-specified name of the policy that threw the fault. xmltojson.XMLtoJSON-1.failed = true

Example error response

{
  "fault": {
    "faultstring": "XMLToJSON[XMLtoJSON-1]: Source xyz is not available",
    "detail": {
      "errorcode": "steps.xml2json.SourceUnavailable"
    }
  }
}

Example fault rule

<faultrule name="VariableOfNonMsgType"></faultrule><FaultRule name="XML to JSON Faults">
    <Step>
        <Name>AM-SourceUnavailableMessage</Name>
        <Condition>(fault.name Matches "SourceUnavailable") </Condition>
    </Step>
    <Step>
        <Name>AM-BadXML</Name>
        <Condition>(fault.name = "ExecutionFailed")</Condition>
    </Step>
    <Condition>(xmltojson.XMLtoJSON-1.failed = true) </Condition>
</FaultRule>

หัวข้อที่เกี่ยวข้อง

JSON เป็น XML: นโยบาย JSON เป็น XML