Author |
Message
|
mrkumar |
Posted: Sat Jul 02, 2016 8:58 am Post subject: JSON array using graphical mapping node |
|
|
 Novice
Joined: 02 Jul 2016 Posts: 18
|
The JSON array object is coming as repeatables when we convert an xml to JSON using graphical mapping node in IIB v 10.0.0.4
for example
{
"sample":{"red"},
"sample":{"blue"},
"sample":{"green"}
}
This is an invalid JSON
desired output is
{
"sample":["red","blue","green"]
}
I am facing this issue while converting a soap response to JSON. So there are so many arrays like this. a prodcut like IIB for sure should have a aumatic converstion of arrays right ?
please correct me if I am wrong or if there is any other way to do it so that i can send the JSON arrays as a response.
I really appreciate your help !  _________________ If you fail, call it as Version1.0  |
|
Back to top |
|
 |
smdavies99 |
Posted: Sat Jul 02, 2016 9:52 am Post subject: |
|
|
 Jedi Council
Joined: 10 Feb 2003 Posts: 6076 Location: Somewhere over the Rainbow this side of Never-never land.
|
Have you looked for similar posts here on this topic.
I can remember JSON arrays being discussed before. _________________ WMQ User since 1999
MQSI/WBI/WMB/'Thingy' User since 2002
Linux user since 1995
Every time you reinvent the wheel the more square it gets (anon). If in doubt think and investigate before you ask silly questions. |
|
Back to top |
|
 |
mrkumar |
Posted: Sat Jul 02, 2016 10:51 am Post subject: |
|
|
 Novice
Joined: 02 Jul 2016 Posts: 18
|
smdavies99 wrote: |
Have you looked for similar posts here on this topic.
I can remember JSON arrays being discussed before. |
I did search for this and find some post for about manual manipulation of individual json array objects. however I want to do know how i can do via graphical mapping node which does things dynamic. Since my web service returns more than one JSON array objects and has many several service operations such as _________________ If you fail, call it as Version1.0  |
|
Back to top |
|
 |
timber |
Posted: Sun Jul 03, 2016 12:43 pm Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
I've posted code that will convert repeating elements into JSON arrays - I believe it's somewhere on the forum in a related post.
Quote: |
a prodcut like IIB for sure should have a automatic conversion of arrays right |
You seem to think this is a simple problem. It is actually a rather tricky problem to solve if you want it to work accurately. You need to work out whether each non-repeating tag is
a) a single tag that is not meant to be an array ( xsd maxOccurs=1 )
or
b) an array with exactly one member ( minOccurs=1, maxOccurs>1 )
That can only be done if the XML is validated against an XSD. You have to use the PSVI information provided by the XML parser. That's not trivial at all. Does that answer your question? |
|
Back to top |
|
 |
mrkumar |
Posted: Sun Jul 03, 2016 7:06 pm Post subject: |
|
|
 Novice
Joined: 02 Jul 2016 Posts: 18
|
timber wrote: |
I've posted code that will convert repeating elements into JSON arrays - I believe it's somewhere on the forum in a related post.
Quote: |
a prodcut like IIB for sure should have a automatic conversion of arrays right |
You seem to think this is a simple problem. It is actually a rather tricky problem to solve if you want it to work accurately. You need to work out whether each non-repeating tag is
a) a single tag that is not meant to be an array ( xsd maxOccurs=1 )
or
b) an array with exactly one member ( minOccurs=1, maxOccurs>1 )
That can only be done if the XML is validated against an XSD. You have to use the PSVI information provided by the XML parser. That's not trivial at all. Does that answer your question? |
you are right. I dont want to restrict the multiple occurances of an object. I want to group them in an array for a JSON format. since we cant have same object name repeated in JSON. _________________ If you fail, call it as Version1.0  |
|
Back to top |
|
 |
mrkumar |
Posted: Tue Jul 05, 2016 3:15 pm Post subject: |
|
|
 Novice
Joined: 02 Jul 2016 Posts: 18
|
mrkumar wrote: |
timber wrote: |
I've posted code that will convert repeating elements into JSON arrays - I believe it's somewhere on the forum in a related post.
Quote: |
a prodcut like IIB for sure should have a automatic conversion of arrays right |
You seem to think this is a simple problem. It is actually a rather tricky problem to solve if you want it to work accurately. You need to work out whether each non-repeating tag is
a) a single tag that is not meant to be an array ( xsd maxOccurs=1 )
or
b) an array with exactly one member ( minOccurs=1, maxOccurs>1 )
That can only be done if the XML is validated against an XSD. You have to use the PSVI information provided by the XML parser. That's not trivial at all. Does that answer your question? |
you are right. I dont want to restrict the multiple occurances of an object. I want to group them in an array for a JSON format. since we cant have same object name repeated in JSON. |
is this a bug in the JSON parser to not have repeated JSON elements in a JSON array object ? _________________ If you fail, call it as Version1.0  |
|
Back to top |
|
 |
timber |
Posted: Tue Jul 05, 2016 11:49 pm Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
Quote: |
is this a bug in the JSON parser to not have repeated JSON elements in a JSON array object ? |
No. This is not a bug. Please review what I said once again.
The JSON parser does not *assume* that two consecutive elements with the same name are *definitely* an array. They might be two occurrences of a non-array element. Having said that, a JSON object should not have two keys with the same name so you could argue that the IIB JSON parser should offer this behaviour as an option.
However...you still have the situation where the input XML contains exactly one occurrence of the tag <myArray> because it just happened to have only one occurrence. What do you want the JSON parser to do about that? Output a JSON field when there is 1 occurrence, and a JSON array when there are 2+ occurrences? |
|
Back to top |
|
 |
mrkumar |
Posted: Wed Jul 06, 2016 10:48 am Post subject: |
|
|
 Novice
Joined: 02 Jul 2016 Posts: 18
|
timber wrote: |
Quote: |
is this a bug in the JSON parser to not have repeated JSON elements in a JSON array object ? |
No. This is not a bug. Please review what I said once again.
The JSON parser does not *assume* that two consecutive elements with the same name are *definitely* an array. They might be two occurrences of a non-array element. Having said that, a JSON object should not have two keys with the same name so you could argue that the IIB JSON parser should offer this behaviour as an option.
However...you still have the situation where the input XML contains exactly one occurrence of the tag <myArray> because it just happened to have only one occurrence. What do you want the JSON parser to do about that? Output a JSON field when there is 1 occurrence, and a JSON array when there are 2+ occurrences? |
ok valid point. that it doesnt know the difference between an array element coming once with that a non array element that comes only once.
but we have to do something for the elements that come as 2+ occurrences right ? since its not a valid JSON anymore.
that we cant have the repeating elements in JSON. because i am getting JSON parser error in browser for that call. it says duplicate element identified.
I am seeing parser error in the browser with duplicate JSON element that occurs more than once.
so as of now, I am creating mapping node to put them in a JSON array and then giving it as response to browser.
hence I am trying to find a generic solution to my 15+ operations in the service. _________________ If you fail, call it as Version1.0  |
|
Back to top |
|
 |
|