Type definition for a smaple json data

4 posts / 0 new
Last post
Offline
Last seen: 5 years 4 months ago
Joined: 03/13/2019
Posts: 6
Type definition for a smaple json data

Hi, 

I find it's difficult to make the Type definition for the json sample data below since the two arrays {} do not have any names.  

"ParameterGroup":[{"Name": "String","Parameter": [ arrays ]},{"Name": "String", "Parameter": [ arrays ],"ParameterGroup": [ arrays ]}]

What kind of Type definition shall be for the json array data without names? 

Thanks.

Keywords:
Fernando Garcia's picture
Offline
Last seen: 3 months 3 weeks ago
Joined: 05/18/2011
Posts: 200

Hi Yanyang,

Do you want to derive the type definition in IDL or XML format from the JSON sample description?

If you want need to have a JSON array composed objects, like in your example; you need to define in your IDL an array or a sequence of a complex type, like a Structure. That structure will contain all the fields of the JSON object within the array. For example:

struct ParameterGroupItem {
    string Name;
};

struct MyStruct {
    sequence<ParameterGroupItem> ParameterGroup;
};

In the example before you have MyStruct, which is the top-level type that you can read or write. That has an field called ParameterGroup that is a sequence of ParameterGroupItems (you could also define ParameterGroup as an array instead).

If you represent a sample for that type definition using JSON, you get something like:

{
    "ParameterGroup": [
        {
            "Name": "AString",
        }
    ]
}

That is, a JSON object representing the top level type, a ParameterGroup array of ParameterGroupItems, and a property called Name in every JSON object representing the ParameterGroupItem.

If you want to nest arrays with objects, like in your example, you have to repeat the process with nested elements.

Is this what you were looking for?

Thanks,
Fernando

Offline
Last seen: 5 years 4 months ago
Joined: 03/13/2019
Posts: 6

Hi Fernando,

I forgot to mention that I wanted to make the type definition in XML format. But I have solved this problem by myself. Thank you for the kind reply!  

Have a nice day,
Yanyang
Fernando Garcia's picture
Offline
Last seen: 3 months 3 weeks ago
Joined: 05/18/2011
Posts: 200

Hi Yanyang,

Great!  I am glad to hear you were able to solve your problem.

Just for reference for those who might find this post looking for a similar solution, the equivalent definition in XML format would be:

<types>
    <struct name= "ParameterGroupItem">
        <member name="Name" stringMaxLength="255" type="string"/>
    </struct>
    <struct name= "MyStruct">
        <member name="ParameterGroup" sequenceMaxLength="100" type="nonBasic" nonBasicTypeName="ParameterGroupItem"/>
    </struct>
</types>

Best regards,
Fernando.