XML to JSON Convertor Using JSON Schema in Java - java

I'm trying to convert an xml to json data using the java-json.jar . The conversion is done. but have issues with the resulting JSON data like
An Integer is expected but the data is converted as String.
it would not create a list if there was only one child element ( even when it should be a list as per the JSON schema ).
Is there any way to convert XML into JSON based on the JSON schema in Java?

Related

How to convert String to Json Object in java or groovy

I saved a json into the database as a string like this:
"[_district:_1_2_5village, _name:_1_1_2id_inter, _gender:_1_3_5sex]"
Now i want to convert it back to a Json Object so as to pick the key and value eg _district is the key and _1_2_5village is the value. Any help on how i can achieve this. Thanks
I tried to convert the string back into JSON by parsing but that dint work for me.
It doesn't work because that's not a JSON format, a JSON is a way of mapping objects and uses key value syntax like so:
{"key": "value"}
and an array would look like this:
[{"key": "value"},{"key": "value"}]
You'll need to make a custom parser for your syntax
Here's the json specification:
https://www.json.org/json-en.html

Convert DynamoJson to something compatible with the Dynamo client

I've got a big dump of DynamoJson e.g.
{"Item": {"id":{"N":"896"}, "name": {"S": "Tom"}}}
I want to parse this JSON and put it to my DynamoDB table...
I've tried:
import com.amazonaws.services.dynamodbv2.document.Item;
Item item = Item.fromJSON(BLOB);
But unfortunately its not smart enough to parse the DynamoDB Json format and doesn't deal with the inner types (S, N etc)... When I try to put I get errors like:
Type mismatch for key id expected: N actual: M
Related Questions:
AWS DynamoDB on Android: Inserting JSON Directly?
This does not work for the DynamoJson format.
Unmarshall DynamoDB JSON
This is exactly what I need but its in NodeJS

Convert csv directly to json with schema and json types

QUESTION: Can you use CsvMapper/CsvSchema to convert csv to a map<string, object> where the object is something other than string, like boolean or integer?
Details: I have some csv files that I want to convert directly to json. I don't have access to the POJO's. But I do have an xml schema file that contains the type information for the columns in the csv data.
After doing some searching I decided to use Jackson's CsvMapper/CsvSchema to convert the csv files to json. I created a CsvSchema with column type information then converted the csv files to a Map<> file then converted the map file to json. It all worked fine except for one thing. All the json properties were Strings, even the properties that the CsvSchema had defined as Boolean and Numeric types. Below is a small snippet that shows what I'm doing.
String csvEmployeeData="\"Bob\",25,true\n" +
"\"Joe\",35,false\n" +
"\"Tom\",45,false\n";
CsvSchema schema = CsvSchema.builder().addColumn("name", ColumnType.String)
.addColumn("age", ColumnType.NUMBER)
.addColumn("isManager", ColumnType.BOOLEAN)
.build();
CsvMapper csvMapper = new CsvMapper();
MappingIterator<Map<String, ? extends Object>> it = csvMapper.readerFor(Map.class)
.with(schema)
.readValues(csvEmployeeData);
List<Map<String, ? extends Object>> objectList=it.readAll();
Map<String, ? extends Object> employeeObj=objectList.get(0);
assertEquals("java.lang.Integer", employeeObj.get("age").getClass().getTypeName());
// Integer age=(Integer)employeeObj.get("age");
// java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
I'm reading the csv data in to a Map where the string is the property name and the Object is value. I thought the value type for Object would be based on the Schema ColumnType. I.e. if it was a ColumnType.BOOLEAN then Object would be Boolean. However all the Objects in the Map are String types.
I was assuming that the CsvSchema ColumnType's would be used in the conversion since in csv everything is a string so you don't need the schema for that. I'm clearly missing something. Surprisingly Google doesn't bring up a lot on using CsvMapper.
Does anyone know if possible to go from CSV to json and use the json types (bool/integer) or does everything have to be a string?

Convert XML with . attribute to JSON in java

I have an XML with the attribute <name.first>.
I want to convert this XML to JSON and save it to MongoDB.
I am using org.json.XML to convert it to JSONObject with:
XML.toJSONObject(xml)
The converted JSON contains the key: { ... , name.first : ... }, which is not supported with MongoDB.
Is there a way to serialize the XML converter so that the conversion would output something else like name_first ?

Extracting Element Value from jPOS ISO

I am trying to extract a value within an ISO String which I get from a jPOS Structured Data. The string looks like this:
221ThirdPartyBillPayment3125
<ThirdPartyBillPayment>
<BillPaymentRequest>
<ReferenceId>1111111111</ReferenceId>
</BillPaymentRequest>
</ThirdPartyBillPayment>
Is there a way I can get the value "1111111111" of ReferenceId node?
The sample data is a postilion structured data field which uses kind of a TLV (tag length value format).
221ThirdPartyBillPayment3125
<ThirdPartyBillPayment>
<BillPaymentRequest>
<ReferenceId>1111111111</ReferenceId>
</BillPaymentRequest>
221ThirdPartyBillPayment
Here 2 is the length of length (21), 21 is the length of the tag ThirdPartyBillPayment
3125
<ThirdPartyBillPayment>
<BillPaymentRequest>
<ReferenceId>1111111111</ReferenceId>
</BillPaymentRequest>
</ThirdPartyBillPayment>
Here 3 is the length of length (125), 125, is the length of data to follow.
You could write code to get access to xml iteratively for all thats available in structured data and then parse out the xml data within.
Or
You could ask Postilion for the dtd/schema for the xml used in their structured data iso field and use jaxb to access the data.
It will boil down to a name value pair
ThirdPartyBillPayment= <ThirdPartyBillPayment><BillPaymentRequest<ReferenceId>1111111111</ReferenceId></BillPaymentRequest>
</ThirdPartyBillPayment>
You've got some custom data in a mix of some fixed fields and some XML there, so you first need to get the whole field off your ISOMsg, i.e:
String s = m.getString("127.1"); // provided your data comes in field 127.1
Then figure out where the XML starts (in this case, at indexOf('<')), then you need feed that XML in an XML parser (you can use jdom that comes as a jPOS dependency), parse the XML and get the child element ReferenceId.

Categories