Hitachi

Cosminexus V11 BPM/ESB基盤 サービスプラットフォーム 開発ガイド 基本開発編


12.3.3 XML→JSON変換の規則

XML→JSON変換の規則について説明します。

表12‒31 XML→JSON変換の規則

入力するXMLファイルの定義

対応するXMLスキーマ

JSON変換結果

<root>false</root>

<xs:element name="root" type="xs:boolean"/>

false

<root>true</root>

true

<root>123</root>

<xs:element name="root" type="xs:decimal"/>

123

<root>123</root>

<xs:element name="root" type="xs:string"/>

"123"

<root xsi:nil="true"/>

<xs:element name="root" type="xs:string" nillable="true"/>※1

null

<root>true</root>

<xs:element name="root" type="xs:boolean"/>

true

<root>

 <field>value</field>

</root>

<xs:element name="root">

 <xs:complexType>

  <xs:sequence>

   <xs:element name="field" type="xs:string"/>

  </xs:sequence>

 </xs:complexType>

</xs:element>

{

 "field":"value"

}

<root>

 <field>value1</field>

 <field>value2</field>

</root>

<xs:element name="root">

 <xs:complexType>

  <xs:sequence>

   <xs:element name="field" maxOccurs="unbounded" type="xs:string"/>※2

  </xs:sequence>

 </xs:complexType>

</xs:element>

{

 "field":[

 "value1",

 "value2"

 ]

}

<root>

 <unnamed_1>

  value1

 </unnamed_1>

 <unnamed_1>

  value2

 </unnamed_1>

</root>

<xs:element name="root" csc:wrapped="true">

 <xs:complexType>

  <xs:choice maxOccurs="unbounded">※2

   <xs:element name="unnamed_1" type="xs:string"/>

  </xs:choice>

 </xs:complexType>

</xs:element>

[

 "value1",

 "value2"

]

<root>

 <d1>

  <d2>value1</d2>

  <d2>value2</d2>

 </d1>

 <d1>

  <d2>value1</d2>

  <d2>value2</d2>

 </d1>

</root>

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<xs:element name="root">

 <xs:complexType>

  <xs:sequence>

   <xs:element name="d1" maxOccurs="unbounded">※2

    <xs:complexType>

     <xs:sequence>

      <xs:element name="d2" maxOccurs="unbounded" type="xs:string"/>※2

     </xs:sequence>

    </xs:complexType>

   </xs:element>

  </xs:sequence>

 </xs:complexType>

</xs:element>

{

 "d1":[

  {"d2":[

   "value1",

   "value2"

 ]},

 {

  "d2":[

   "value3",

   "value4"

  ]}

 ]

}

<xs:element name="root" csc:wrapped="true">

 <xs:complexType>

  <xs:choice maxOccurs="unbounded">※2

   <xs:element name="d1" csc:wrapped="true">

    <xs:complexType>

     <xs:choice maxOccurs="unbounded">※2

      <xs:element name="d2" type="xs:string"/>

     </xs:choice>

    </xs:complexType>

   </xs:element>

  </xs:choice>

 </xs:complexType>

</xs:element>

[

 ["value1","value2"],

 ["value3","value4"]

]

[図データ] 

[図データ] 

[図データ] 

[図データ] 

注※1 typeは任意タイプを指定できます。

注※2 maxOccursの指定値は2以上の最大配列要素数です。unboundedは無制限の配列要素数を表します。

〈この項の構成〉

(1) XML要素の変換

XML要素ごとに対応するXMLスキーマ要素を突き合わせて変換します。XML要素に対応するXMLスキーマ要素がない場合,該当するXML要素をスキップし,エラーリストに登録します。

ルート要素にマッチするXMLスキーマのルート要素がない場合のAPIメソッドの返り値はnullとなります。

(2) データ型の対応

対応するXMLスキーマ要素のデータタイプによって,変換されるJSONのデータ型が異なります。

変換元XMLタイプと変換先のJSONのデータ型の対応を次に示します。

表12‒32 変換元XMLタイプと変換先のJSONのデータ型の対応

変換元XMLタイプ

変換先JSONデータ型

anyURI

string

base64Binary

string

boolean

true,false

date

string

dateTime

string

decimal

number

integer

number

long

number

int

number

short

number

byte

number

nonNegativeInteger

number

positiveInteger

number

unsignedLong

number

unsignedInt

number

unsignedShort

number

unsignedByte

number

nonPositiveInteger

number

negativeInteger

number

double

number

duration

string

float

number

gDay

string

gMonth

string

gMonthDay

string

gYear

string

gYearMonth

string

hexBinary

string

NOTATION

string

QName

string

string

string

normalizedString

string

token

string

language

string

Name

string

NCName

string

ENTITY

string

ID

string

IDREF

string

NMTOKEN

string

time

string

ENTITIES

string

IDREFS

string

NMTOKENS

string

XMLスキーマ仕様で定義されていないデータタイプについては,派生元の組み込みデータ型を摘出して変換します。

complexTypeは,次のデータ型に変換されます。

表12‒33 complexTypeの変換規則

変換元XMLタイプ

変換先JSONデータ型

<complexType><sequence>

object

<complexType><all>

object

<complexType><choice>

  • maxOccurs>1の場合は,JSON Array

  • maxOccurs≦1の場合は,子要素のデータ型

sequenceの子要素として定義された場合以外は子要素の要素名は無視されます。

complexTypeのchoice要素の変換例を次に示します。

表12‒34 complexTypeのchoice要素の変換例

入力するXMLファイルの定義

対応するXMLスキーマ

JSON変換結果

<root>

 <f1>0</f1>

</root>

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:choice>

    <xs:element name="f1" type="xs:byte"/>

    <xs:element name="f2" type="xs:string"/>

   </xs:choice>

  </xs:complexType>

 </xs:element>

</xs:schema>

0

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<root>

 <f2>1</f2>

</root>

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:choice>

    <xs:choice>

     <xs:element name="f1" type="xs:byte"/>

    </xs:choice>

    <xs:choice>

     <xs:element name="f2" type="xs:string"/>

    </xs:choice>

   </xs:choice>

  </xs:complexType>

 </xs:element>

</xs:schema>

"1"

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<root>

 <f2>1</f2>

</root>

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:choice maxOccurs="unbounded">

    <xs:element name="f1" type="xs:byte"/>

    <xs:element name="f2" type="xs:string"/>

   </xs:choice>

  </xs:complexType>

 </xs:element>

</xs:schema>

["1"]

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<root>

 <f1>0</f1>

 <f2>2</f2>

 <f1>1</f1>

 <f2>3</f2>

</root>

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:sequence>

    <xs:choice maxOccurs="unbounded">

     <xs:element name="f1" type="xs:byte"/>

     <xs:element name="f2" type="xs:string"/>

    </xs:choice>

   </xs:sequence>

  </xs:complexType>

 </xs:element>

</xs:schema>

{

 "f1":[0,1],

 "f2":["2","3"]

}

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

(3) simpleTypeのlist要素の変換

simpleTypeのlist要素では,指定したデータ型を持つ値のリスト(スペース区切り)を要素値として定義できます。変換元のXMLのデータ型がlist要素に指定されている場合,XMLデータ型に関係なくJSONのstringに変換されます。

表12‒35 simpleTypeのlist要素の変換例

入力するXMLファイルの定義

対応するXMLスキーマ

JSON変換結果

<root>1 2 3 4 5</root>

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:simpleType>

   <xs:list itemType="xs:integer"/>

  </xs:simpleType>

 </xs:element>

</xs:schema>

"1 2 3 4 5"

[図データ] 

[図データ] 

[図データ] 

[図データ] 

(4) simpleTypeのunion要素の変換

simpleTypeのunion要素では,複数のデータ型を持つ要素を定義できます。XML→JSON変換では,複数のデータ型を定義順に要素値が変換できるか確認し,最初に変換できると判定したデータ型でJSONデータを変換します。simpleTypeのunion要素を使用したXMLスキーマを利用し,JSON→XML変換後にXML→JSON変換を行った場合,変換結果は元のJSON形式データと異なる場合があります。

表12‒36 simpleTypeのunion要素の変換例

入力するXMLファイルの定義

対応するXMLスキーマ

JSON変換結果

<root>1</root>

<xs:schema …>

 <xs:element name="root">

  <xs:simpleType>

   <xs:union memberTypes="xs:byte xs:string"/>

  </xs:simpleType>

 </xs:element>

</xs:schema>

1

<root>abc</root>

[図データ] 

[図データ] 

"abc"

[図データ] 

[図データ] 

<root>1</root>

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:simpleType>

   <xs:union memberTypes="xs:string xs:byte"/>

  </xs:simpleType>

 </xs:element>

</xs:schema>

"1"

[図データ] 

[図データ] 

[図データ] 

注 斜体は,複数のデータ型を持つ要素を表します。

(5) サポートするXMLスキーマ要素

XML→JSON変換がサポートするXMLスキーマ要素の組み合わせは,JSON→XML変換がサポートするXMLスキーマ要素と同じです。

JSON→XML変換がサポートするXMLスキーマ要素については,「12.3.2(4) サポートするXMLスキーマ要素」を参照してください。

(6) 属性データへの変換

complexTypeのsequence要素に,attribute属性が定義されている場合,属性名をフィールド名,属性値をフィールド値としたJSON Objectのフィールドに変換されます。

complexTypeのsimpleContent要素に,attribute属性が定義されている場合,「要素名@属性名」をフィールド名,属性値をフィールド値としたJSON Objectのフィールドに変換されます。該当する要素が複数あり,配列にまとめる場合,属性値も配列にまとめられます。だたし,simpleContent要素がルート要素の場合,属性は変換されません。attribute属性が定義されていない場合は変換されません。エラーリストにも登録されません。

属性名と同名のフィールドがある場合,変換は行われないで,該当する属性がエラーリストに登録されます。

表12‒37 属性データへの変換例

入力するXMLファイルの定義

対応するXMLスキーマ

JSON変換結果

<root attr1="123" attr2="value3">

 <data>value1</data>

</root>

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:sequence>

    <xs:element name="data" type="xs:string"/>

   </xs:sequence>

   <xs:attribute name="attr1" type="xs:decimal"/>

   <xs:attribute name="attr2" type="xs:string"/>

  </xs:complexType>

 </xs:element>

</xs:schema>

{

 "data":"value1",

 "attr1":123,

 "attr2":"value3"

}

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:sequence>

    <xs:element name="data" type="xs:string"/>

   </xs:sequence>

  </xs:complexType>

 </xs:element>

</xs:schema>

{

 "data":"value1"

}

[図データ] 

[図データ] 

[図データ] 

<root>

 <field1 attr="a1">

  value1

 </field1>

 <field2>value2</field2>

</root>

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:sequence>

    <xs:element name="field1">

     <xs:complexType>

      <xs:simpleContent>

       <xs:extension base="xs:string">

        <xs:attribute name="attr" type="xs:string"/>

       </xs:extension>

      </xs:simpleContent>

     </xs:complexType>

    </xs:element>

    <xs:element name="field2" type="xs:string" />

   </xs:sequence>

  </xs:complexType>

 </xs:element>

</xs:schema>

{

 "field1":"value1",

 "field1@attr":"a1",

 "field2":"value2"

}

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<root>

 <field1 attr="a1">

  value1

 </field1>

 <field1>

  value1

 </field1>

 <field1 attr="a3">

  value1

 </field1>

 <field2>value2</field2>

</root>

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:sequence>

    <xs:element name="field1" maxOccurs="unbounded">

     <xs:complexType>

      <xs:simpleContent>

       <xs:extension base="xs:string">

        <xs:attribute name="attr1" type="xs:string"/>

       </xs:extension>

      </xs:simpleContent>

     </xs:complexType>

    </xs:element>

    <xs:element name="field2" type="xs:string" />

   </xs:sequence>

  </xs:complexType>

 </xs:element>

</xs:schema>

{

 "field1":[

  "value1",

  "value2",

  "value3"

 ],

 "field1@attr":[

  "a1",

  null,

  "a3"

 ],

 "field2":"value2"

}

[図データ] 

[図データ] 

[図データ] 

(7) 配列データの変換(アンラップ変換およびラップ変換)

配列データをアンラップ変換およびラップ変換で変換できます。

XML要素に対応するelement要素のmaxOccurs属性に2以上が設定されている場合,JSON arrayに変換できます(アンラップ変換)。

配列の変換は対象のelement要素のタイプが,complexTypeのchoice要素の場合,子要素の値を親要素のJSON arrayに変換できます(ラップ変換)。

表12‒38 配列の変換例

入力するXMLファイルの定義

対応するXMLスキーマ

JSON変換結果

<root>

 <field>data1</field>

 <field>data2</field>

</root>

<!--アンラップ変換 -->

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:sequence>

    <xs:element name="field" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

   </xs:sequence>

  </xs:complexType>

 </xs:element>

</xs:schema>

{

 "field":[

   "data1",

   "data2"

 ]

}

<root>

 <field>data1</field>

</root>

{

 "field":[

   "data1"

 ]

}

<root>

 <field>data1</field>

 <field>data2</field>

</root>

<!--ラップ変換 -->

<xs:schema …>

 <xs:element name="root" csc:wrapped="true">

  <xs:complexType>

   <xs:choice minOccurs="0" maxOccurs="unbounded">

    <xs:element name="field" type="xs:string"/>

   </xs:choice>

  </xs:complexType>

 </xs:element>

</xs:schema>

[

 "data1",

 "data2"

]

<root>

 <field>data1</field>

</root>

[

 "data1"

]

(8) 要素名の別名を使用した変換

element要素のcsc:jsonfield属性に「JSONフィールド名」を設定すると,対応する要素のJSONフィールド名を「JSONフィールド名」に変更できます。

表12‒39 別名を使用した変換例

入力するXMLファイルの定義

対応するXMLスキーマ

JSON変換結果

<root>

 <_field_>data</_field_>

</root>

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:sequence>

    <xs:element csc:jsonfield="&lt;field&gt;" name="_field_" type="xs:string"/>

   </xs:sequence>

  </xs:complexType>

 </xs:element>

</xs:schema>

{

 "<field>":"data"

}

[図データ] 

[図データ] 

[図データ] 

[図データ] 

(9) defaultまたはfixedの変換

element要素にdefaultまたはfixed属性が指定され,次の条件を満たす場合,JSONフィールドが生成されます。

defaultまたはfixed属性を指定したXMLスキーマを利用し,JSON→XML変換後にXML→JSON変換を行った場合,変換結果は元のJSON形式データと異なる場合があります。

表12‒40 defaultまたはfixedの変換例

入力するXMLファイルの定義

対応するXMLスキーマ

JSON変換結果

<root>

 <name>dogName</name>

</root>

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType>

   <xs:sequence>

    <xs:element name="name" type="xs:string"/>

    <xs:element fixed="animal"

name="type" type="xs:string"/>

    <xs:element default="dog"

name="kind" type="xs:string"/>

    <xs:element minOccurs="0" default="French bull"

name="subkind" type="xs:string"/>

   </xs:sequence>

  </xs:complexType>

 </xs:element>

</xs:schema>

{

 "name":"dogName",

 "type":"animal",

 "kind":"dog"

}

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

[図データ] 

(10) mixed contentの変換

mixed属性にtrueが指定されたComplexType要素に対応するXML要素に,テキストデータが設定されている場合,JSON Objectの#textフィールドに該当するテキストデータが変換されます。

表12‒41 mixed contentの変換例

入力するXMLファイルの定義

対応するXMLスキーマ

JSON変換結果

<root>

 <field>data1</field>

 data2

</root>

[図データ] 

[図データ] 

[図データ] 

<xs:schema …>

 <xs:element name="root">

  <xs:complexType mixed="true">

   <xs:sequence>

    <xs:element name="field" type="xs:string"/>

   </xs:sequence>

  </xs:complexType>

 </xs:element>

</xs:schema>

{

 "field":"data1",

 "#text":"data2"

}

[図データ] 

[図データ] 

[図データ]