Tim D'haeyer
Tim D'haeyer

Reputation: 694

BizTalk json encoder not using the correct type

I'm trying to send a quite simple JSON message from BizTalk.

{
  "Value": 1
}

I set the type of the "Value" field to xs:int in my xml schema

<xs:element minOccurs="1" maxOccurs="1" name="Value" type="xs:int" />

But it keeps generating the wrong JSON message.

{
  "Value": "1"
}

Not sure what I'm doing wrong here. Does anybody have some tips?

Upvotes: 0

Views: 1125

Answers (3)

Michael Coxon
Michael Coxon

Reputation: 5510

Using part of Dijkgraafs answer I have worked out that along with this...

...Your XML payload is not going through a XML Dissasembler or Assembler before reaching the JSON Encoder, and hence the Message Type isn't set correctly, in which case it is not using the schema to determine the correct date types...

...you also need to set the AddXmlDeclaration value on the XML Assembler/Disassembler to False.

Upvotes: 0

Piotr Grudzień
Piotr Grudzień

Reputation: 189

Biztalk Embedded JSON Encoder has the ability to recognize schema of XML processed and should recognize xs:int value from schema and encoded without quotes.

But sometimes (as Sandro Pereira indicates even often) this encoder throws the error:

Reason: Value cannot be null. Parameter name: key

That was my case. I had to use a custom JSON Encoder that recognizes XML datatype somehow.

My solution was:

  1. Use this build of Newtonsoft.Json library that uses json:Type argument as indicator of XML tag datatype.

  2. Create a custom JSON Encoder that uses the previous library

  3. Create custom Complex Type in separate schema with targetNamespace = "http://james.newtonking.com/projects/json" Note that attribute Type need to be Qualified Custom Type

  4. Import above schema in Your target schema using prefix json

Prefix Json

  1. Use this type as Data Structure Type and rename Your parameter.

Custom Record Custom Type

  1. Map this checking logical existance, to avoid putting argument without value or custom xslt tranform

Logical Existance

Usefull sites: Adding name to attribute

Upvotes: 1

Dijkgraaf
Dijkgraaf

Reputation: 11527

There can be multiple issues at work here.

  1. Your XML payload is not going through a XML Dissasembler or Assembler before reaching the JSON Encoder, and hence the Message Type isn't set correctly, in which case it is not using the schema to determine the correct date types.

  2. You have another earlier element in your schema with the same name but a different type. It will use the type from the first one for all elements with the same name. This was a bug in BizTalk 2013 R2, and I think it was still there in BizTalk 2016 and could cause some strange errors, especially if you had a Records (complex types) and Element (simple types) with the same name. e.g FIX: JSON encoder unable to handle XML schema with the same name for record and one of its elements in CU 7

Upvotes: 0

Related Questions