XML to JSON Conversion Issue with attribute in array - json

I need to convert the xml input to JSON input.where I need place the value of extensionField (conversion tools are giving different conversion - xml to JSON converter).Please let me know.
*XML format*
<transactionExtensions>
<extensionField number="1">Value1</extensionField>
<extensionField number="n">Valuen</extensionField>
</transactionExtensions>
*JSON Conversion:*
transactionExtensions": { "extensionField": [ {"number":"1"},{"number":"n"}]}

Related

How to parse nested json-array in streaming fashion with zio-json

For a json array like this:
[
my-json-obj1,
my-json-obj2,
my-json-obj3,
....
my-json-objN
]
And MyJsonObj class that represents a mapping of single object in array I can say:
val myJson = '''[...]'''
ZStream
.fromIterable(myJson.toSeq)
.via(JsonDecoder[MyJsonObj].decodeJsonPipeline(JsonStreamDelimiter.Array))
to parse that array in a "streaming" way, i.e. emit mapped objects as they are parsed form the input as opposed to reading all the input first and then extracting the objects.
How can I do the same if the array is nested inside a json object say like this?:
{
"hugeArray":
[
my-json-obj1,
my-json-obj2,
my-json-obj3,
....
my-json-objN
]
}
I trawled through zio-json source code, but I can't find any foothole there for this use case. I guess I could carve out that array from the json document and feed that to decodeJsonPipeline. Is there any better, json-syntax aware way of doing this? If not directly in zio-json perhaps with help of some other open source json libraries?

Axis2 JSon Support (Jettison) bad xml conversion

I am using Axis2 1.6.4 to implement REST Json WebServices (https://axis.apache.org/axis2/java/core/docs/json_support.html) and I face an issue when Jettison converts Json object To XML if it does not have a "root" element. Details:
If request is:
{"name":"John","age":30}
Then XML OMElement at server side is:
<name>John</name
So age element is missed
Instead, if request is:
{person:{"name":"John","age":30}}
Then XML OMElement at server side is:
<person><name>John</name><age>30</age></person>
Thanks for your help,
Martí
I've founda work around to overcome this JSON->XML conversion issue. Thanks to this post:How to use Axis2 JSON, I've realized I am able to access input json before converting it to XML, and also to create an OMElement from a json string. So when input is a json request, I wrap it with a json "root" element, and then convert it to XML without losing data.
If it could be useful to someone, below is the source code to get input json string and the source code to convert json string to OMElement
Get input json string and wrap into a root
OMSourcedElement source=(OMSourcedElement )msg;
AbstractJSONDataSource jsonSoucre=(AbstractJSONDataSource)source.getDataSource();
MessageContext msgCtxt= MessageContext.getCurrentMessageContext();
JSONDataSource jsonRequestEnvDS= new JSONDataSource(new StringReader("{\"JSONEnvelope\": " + jsonSoucre.getObject() + " }"), msgCtxt);
OMFactory factory = OMAbstractFactory.getOMFactory();
OMSourcedElement omRequest = factory.createOMElement(jsonRequestEnvDS,null,null);
Create an OMElement from a json string
String jsonString="{...}";
JSONDataSource jsonDS= new JSONDataSource(new StringReader(jsonString),msgCtxt);
factory = OMAbstractFactory.getOMFactory();
OMSourcedElement resonseJson = factory.createOMElement(jsonDS,null,null);
return resonseJson;

Get the JSON from HttpEntity

I am using akka.http.scaladsl.model.HttpResponse, HttpEntity.
After getting the response , it is of type responseEntity of the format (Content-type: 'application/json', {MyJSONHERE}). Is there a way I can extract my json from the entity.
I tried entity.getDataBytes which gives the content of the entity in ByteString format. I want to properly read the JSON and parse it. Can someone guide me on this?
Code below works for me
entity.dataBytes.runWith(Sink.fold(ByteString.empty)(_ ++ _)).map(_.utf8String) map { result =>
JsonMethods.parse(result)
}
dataBytes returns Source[ByteString, Any], Sink.fold combines all parts of the stream into one ByteString and utf8String converts ByteString into usual String.
Here is some useful docs about HttpEntity.
Can you try below code?
entity.getDataBytes.utf8String
That would return String representation of JSON.

How to convert a javascript object to DyanamoDb JSON format?

JSON.stringify(obj) converts the object into normal JSON format.
How to convert a javascript object to DyanamoDb JSON format ?
Not sure what you mean by "DynamoDB JSON format".
JSON, is JSON, but here is an example of putting a JSON object into DynamoDB:
Document doc = Document.FromJson( itemJson );
table.PutItem( doc);
This library does exactly what I wanted to do:
"Translates sane javascript objects (and JSON) into DynamoDb format and vice versa."
https://www.npmjs.com/package/dynamodb-marshaler

How to convert a String array to Swiftyjson JSON type

I need to pass an array of string/Int (doesnt matter) as JSON parameter in a HTTP body for a POST request.
{
"par1" : value,
"par2" : "value2",
"par3" : ['123:456', '123:234' ...]*
}
*My problem is for filling my JSON object with param3 values. In the swift code I have them as an array of Strings.
There are many example of converting a JSON object back to an array of strings/Int, but I can't find it the other way around.
As mentioned in the comments, the way to go is to use NSJSONSerialization to generate object encoded in JSON data.