Hi guys,
I am working on a set of services where I need to convert from xml to json format.
I have been using the built-in JSON step as I prefer using the builtin steps rather than custom c# code .
The interface exposed by the service and agreed with the consumer have an object array in the output. The step by default convert the incoming xml into a json array if the corresponding xml node has more than one element, but if there is only one element than it result in a json object.
For Example
<contractLines>
<lineId>35639d01-cb69-4d0d-8747-164e33a15aec</lineId>
</contractLines>
<contractLines>
<lineId>1734312f-24df-48b6-8de6-4fed70560efa</lineId>
</contractLines>
==> JSON
"contractLines": [
{
"lineId": "35639d01-cb69-4d0d-8747-164e33a15aec",
},
{
"lineId": "1734312f-24df-48b6-8de6-4fed70560efa",
}
]
But when only one line is available it generates a simple Json object...
<contractLines>
<lineId>35639d01-cb69-4d0d-8747-164e33a15aec</lineId>
</contractLines>
==> JSON
"contractLines":
{
"lineId": "35639d01-cb69-4d0d-8747-164e33a15aec",
}
I saw on the Newtonsoft documentation that is possible to override this behavior using a special tag,
<contractLines json:Array="true" xmlns:json="http://james.newtonking.com/projects/json">
and in this way it always generate an array.
The builtin step does not seem to implement this feature, but it might be an improvement in order to avoid writing custom code ...
What do you think?
Is it feasible to add it?
Thank you
F.
Tags: json
can you send me the link to the newtonsoft documentation that describes this?
Fabrizio,
This works the way you want it to. I just used this XML with the JSON step:
<contractLines json:Array="true" xmlns:json="http://james.newtonking.com/projects/json">
<lineId>35639d01-cb69-4d0d-8747-164e33a15aec</lineId>
</contractLines>
And this is the JSON output:
{
"contractLines": [{
"lineId": "35639d01-cb69-4d0d-8747-164e33a15aec"
}]
}
Notice that lineId has been put into an array. Are you expecting a different output?
Joe
@Martin, this is the link to the documentation
http://www.newtonsoft.com/json/help/html/ConvertXmlToJsonForceArray...
@Joe, yes, you're right. It works but I had set the option Remove Xml Namespace = true and this probably removes the newtonsoft namespace before the conversion, resulting in the following json
{"item":
{"@Array":"true",
"itemNumber":"6696903"
}
}
ok....I think I have a modification in that will make this work. I'll send you a build today to test
build is taking a bit longer...found another weird bug.
Neuron ESB Product Support Forums and Communities
© 2024 Created by Neuron Admin. Powered by