Failure at HDS to JSON conversion ?!

Poster Content
nk4um Administrator
Posts: 607
November 27, 2013 16:23

Hi Radu,

yes this accessor doesn't support that feature at the moment. If you look at the XML equivalent accessor: http://docs.netkernel.org/book/view/book:json:core/doc:json:core:JSONToXML it takes a configuration operator that allows the root to be removed. It would be relatively easy to port this feature across. Fancy having a go?

Cheers, Tony

Like · Post Reply
nk4um User
Posts: 37
November 26, 2013 09:44Still a (small) problems ...

Thanks for feedback!

Now I managed to produce the JSON array.

From

<patient__A>
  <patient>
    <id>7001&lt;/id&gt;
    <lastname>Găvan&lt;/lastname&gt;
    <firstname>Cristina-Mădălina&lt;/firstname&gt;
    <gender>F&lt;/gender&gt;
    <cnp>2560607142376&lt;/cnp&gt;
  &lt;/patient&gt;
  <patient>
    <id>4001&lt;/id&gt;
    <lastname>Gheorghe&lt;/lastname&gt;
    <firstname>Andrei&lt;/firstname&gt;
    <gender>M&lt;/gender&gt;
    <cnp>1920427167682&lt;/cnp&gt;
  &lt;/patient&gt;
&lt;/patient__A&gt;

now I got using active:JSONFromHDS

{"patient": [
    {
        "cnp": "2560607142376",
        "firstname": "Cristina-Mădălina",
        "gender": "F",
        "id": "7001",
        "lastname": "Găvan"
    },
    {
        "cnp": "1920427167682",
        "firstname": "Andrei",
        "gender": "M",
        "id": "4001",
        "lastname": "Gheorghe"
    }
]

My question is if I could obtain instead

[
    {
        "cnp": "2560607142376",
        "firstname": "Cristina-Mădălina",
        "gender": "F",
        "id": "7001",
        "lastname": "Găvan"
    },
    {
        "cnp": "1920427167682",
        "firstname": "Andrei",
        "gender": "M",
        "id": "4001",
        "lastname": "Gheorghe"
    }
]

It is that possible?! I know I could make an workaround (just to process the output String) but I'm searching a more elegant solution.

Posted by tab (View)
Hi Radu, see the documentation page for JSONFromHDS:

http://docs.netkernel.org/book/view/book:json:core/doc:json:core:JSONFromHDS

What you need to do is use a naming convention like this to cause an array to be created. This is the most consistent way to do the conversion when you consider the need to support arrays of length zero or one.

<patient__A>
  <patient>
    <id>1&lt;/id&gt;
  &lt;/patient&gt;
  <patient>
    <id>2&lt;/id&gt;
  &lt;/patient&gt;
&lt;/patient__A&gt;

Cheers, Tony

Like · Post Reply
nk4um Administrator
Posts: 607
August 28, 2013 12:15

Hi Radu, see the documentation page for JSONFromHDS:

http://docs.netkernel.org/book/view/book:json:core/doc:json:core:JSONFromHDS

What you need to do is use a naming convention like this to cause an array to be created. This is the most consistent way to do the conversion when you consider the need to support arrays of length zero or one.

<patient__A>
  <patient>
    <id>1&lt;/id&gt;
  &lt;/patient&gt;
  <patient>
    <id>2&lt;/id&gt;
  &lt;/patient&gt;
&lt;/patient__A&gt;

Cheers, Tony

Like · Post Reply
nk4um User
Posts: 37
August 28, 2013 11:19Failure at HDS to JSON conversion ?!

Hi all!

Just tried

<endpoint>
  <grammar>res:/patients/typeahead-demo&lt;/grammar&gt;
  <request>
    <identifier>active:JSONFromHDS&lt;/identifier&gt;
    <argumentname="operand">
      <literaltype="xml">
        <patients>
          <patient>
            <id>1&lt;/id&gt;
          &lt;/patient&gt;
          <patient>
            <id>2&lt;/id&gt;
          &lt;/patient&gt;
        &lt;/patients&gt;
      &lt;/literal&gt;
    &lt;/argument&gt;
    <representation>java.lang.String&lt;/representation&gt;
  &lt;/request&gt;
&lt;/endpoint&gt;

and got at

res:/patients/typeahead-demo

the unexpected result:

{"patients": {"patient": {"id": "2"}}}

So, instead of putting the children in an array, the converter overwrite them one uppon another and only the last child info appears in the result.

There is a declarative way of doing that works ?!

(I mean only with xml in module.xml ?! )

Please advice.

Radu

Like · Post Reply