Json to XML using ruby - json

I'm converting the Json to XML in ruby using .to_xml conversion
as part of xml tag getting '-' instead of '_'.
JSON used:
{
"Test_xml": {
"user_Details": {
"UserName": "abcd",
"Password": 1234
}
}
}
Expected XML:
<Test_xml>
<user_Details>
<UserName>abcd</UserName>
<Password>1234</Password>
</user_Details>
</Test_xml>
But got this as XML:
<Test-xml>
<user-Details>
<UserName>abcd</UserName>
<Password>1234</Password>
</user_Details>
</Test-xml>
In above case instead of _ we are receiving - as part of xml tag

Related

Save JSON with special characters (currency symbols)

I'm using Scrapy to extract some values from a website. One of the fields is "price" and I have something like:
...
item = {
"title": title,
"url" : url,
"price": {
"symbol": the_currency_symbol,
"amount": float_number
}
}
yield item
I set the output to be a JSON file and I yield a dictionary item.
The problem is that when I open the output JSON with the items I see this:
{
"title": title,
"url" : url,
"price": {
"symbol": "\u00a3",
"amount": 12.99
}
}
How can I see the correct currency symbol in the JSON file?
Scrapy normally produces JSON feeds in ASCII encoding. Your JSON file has correct data but to see the currency symbol properly you can convert the output JSON file to UTF-8 encoding.
You can make Scrapy generate JSON in UTF-8 encoding by setting FEED_EXPORT_ENCODING="utf-8". For more help, see the answers to the question: Scrapy json response convert in utf-8 encode and Scrapy documentation https://docs.scrapy.org/en/latest/topics/feed-exports.html#std-setting-FEED_EXPORT_ENCODING.
If you do not want to run the scraper again then you can use some tool like https://stedolan.github.io/jq/ on your JSON file like jq file.json > outfile.json. Then outfile.json will have proper symbols.

How to handle data type wise XML to JSON conversion by using NewtonSoft

I am using NewtonSoft namespace to convert the xml data to json. I want the json data value to preserve the data type. But, by using "SerializeXmlNode" it is giving json data value in string.
XML Data is:
<statecode>1</statecode>
<iskit>false</iskit>
<name>PokeMon go</name>
<ExpirationDate>2013-12-31T00:00:00</ExpirationDate>
<price>25.00</price>
I am using the following code:
string requestData = #"<products><statecode>1</statecode>
<iskit>false</iskit>
<name>PokeMon go</name>
<ExpirationDate>2013-12-31T00:00:00</ExpirationDate>
<price>25.00</price></products>";
XmlDocument transformData = new XmlDocument();
transformData.LoadXml(requestData);
foreach(XmlNode data in transformData.GetElementsByTagName("products"))
{
string transformedJsonData = JsonConvert.SerializeXmlNode(data);
}
Output:
{
"statecode" : "1",
"iskit" : "false",
"name":"PokeMon go",
"ExpirationDate":"2013-12-31T00:00:00",
"price":"25.00"
}
Expected Output:
{
"statecode" : 1,
"iskit" : false,
"name":"PokeMon go",
"ExpirationDate":"2013-12-31T00:00:00",
"price": 25.00
}
You can observe output is in string.

Mule (Dataweave) transform JSON array to string

This is probably simple, but everything I find in a search is only vaguely related to my question.
I have this:
{
"user":"C03999",
"caseNumber":"011-1234567",
"documents":[
{
"file":[
{"name":"indem1","document":"xSFSF%%GSF","mimeType":"PDF"},
{"name":"indem2","document":"xSFSF%%GSF","mimeType":"PDF"}
]
}
],
"mortgagee":"22995",
"billTo":"Originator",
"agreementDate":"2016-11-25",
"term":360,
"expirationDate":"2017-11-25",
"startDate":"Agreement",
"lenderEndorsed":true,
"lenderExecutedAgreement":false,
"indemnificationAgreementTransferable":false,
"qadLrsFileNumber":"2016-99999-9999",
"docketNumber":"2016-9999-99"
}
I would like to get this string out of it:
indem1,indem2
I created a global function:
<configuration doc:name="Configuration">
<expression-language autoResolveVariables="true">
<global-functions>
def convertToString(data){
return data.toString();
}
</global-functions>
</expression-language>
</configuration>
My transform looks like this:
%dw 1.0
%output application/csv
---
payload.documents.file map {
"" : convertToString($.name)
}
My output looks like:
[indem1\, indem2]
What do I need to do to get my desired string (indem1,indem2)?
Your question title says that " JSON array To string" but the dataweave code attached above has application/csv in the output.
Are you trying to convert into csv?? If that is the expectation, comma is a reserved character for .csv format and that is the reason , is getting escaped with backslash[indem1\,indem2].
If your intention is to convert into java, here is the code that returns String value.
%dw 1.0
%output application/java
payload.documents[0].file.*name joinBy ','
In general, if you have an object, and you need PART of that object to be written in a given format / MIME-type, use the write() function in the dw::Core module. If you need to read some content in an object that is JSON/JAVA/CSV and the rest is some other format, use the read() function in the same module.
For example, suppose you have payload as:
{
field1: "foo",
field2: { nested: "value" }
}
Further, suppose that you want the object to be JAVA, but the "field2" value to be seen as JSON. You can use this DW:
output application/java
---
{
field1: payload.field1,
field2: write(payload.field2,'application/json')
}
The result is:
{
field1: "foo",
field2: '{ "nested": "value" }'
}
NOTICE that "field2" now shows up as a nested JSON object, not a JAVA object. One way to tell is that the key in the JSON object is in quotes, but in a JAVA object they are not.

How do I programmatically create JSON in XQuery in MarkLogic?

I need to build up a JSON node in XQuery in MarkLogic. I know that I can use xdmp:unquote() to parse from a string into a node(). However, I’d like to build the JSON programmatically, without ugly string concatenation. I can use computed element constructors to build XML nodes in XQuery. Is there something similar for JSON nodes?
JSON is implemented in MarkLogic as an extension to the XML data model. MarkLogic 8 introduces object-node, array-node, number-node, boolean-node, and null-node tests and constructors. Thus, in XQuery you can build JSON with computed constructors, just like you would with XML. For example,
object-node {
"key" || fn:string(xdmp:random(100)): array-node { 1, 2, 3 },
"another": object-node { "child": text {'asdf'} },
"lastButNotLeast": boolean-node { fn:true() }
}
will create the JSON,
{
"key47": [1, 2, 3],
"another": {
"child": "asdf"
},
"lastButNotLeast": true
}
Aside: In JavaScript you can build JSON-like structures as JavaScript objects using JavaScript syntax. You can convert a JavaScript object into a JSON node using xdmp.toJSON(). Most builtin functions that require a JSON node, however, will do this conversion automatically, such as xdmp.documentInsert().

Converting a XML string into a JSON string is not giving desired outcome

I am using NewtonSoft.JSON for parsing a XML document and building a JSON string But I am not getting desired output. My xml string is given below
<fulfillment>
<tracking_number/>
<line_items>
<id>466157049</id>
<quantity>1</quantity>
</line_items>
</fulfillment>
And after converting the XML to JSON it is giving the below JSON
{"fulfillment":{"tracking_number":"er2222222","line_items":{"id":"464194075","quantity":"1"}}}
But I need the JSON as below as my XML cam contains multiple line_items data.The difference between above and below JSON is the array bracket after line items element. when I am using two line_items in the xml it is giving the desired result but with single line_items it is not returning array like JSON format in the line_items section.
{"fulfillment":{"tracking_number":"er2222222","line_items":[{"id":"464194075","quantity":"1"}]}}
What is the way out to resolve the issue.
Thanks
Utpal Maity
You need to do a few changes in the xml. It should look like this:
<fulfillment xmlns:json='http://james.newtonking.com/projects/json'>
<tracking_number>er2222222</tracking_number>
<line_items json:Array='true'>
<id>466157049</id>
<quantity>1</quantity>
</line_items>
</fulfillment>
Serializing this xml sample with JSON.NET will result in the json string you need. Adding multiple line_items nodes will give the expected result. Example with multiple line_items:
<fulfillment xmlns:json='http://james.newtonking.com/projects/json'>
<tracking_number>er2222222</tracking_number>
<line_items json:Array='true'>
<id>466157049</id>
<quantity>1</quantity>
</line_items>
<line_items>
<id>466157050</id>
<quantity>2</quantity>
</line_items>
</fulfillment>
Calling the following code with above xml sample:
var xml = new XmlDocument();
xml.Load("Location of the xml file");
var jsonStr = JsonConvert.SerializeXmlNode(xml);
will result in the following json string:
{
"fulfillment": {
"tracking_number": "er2222222",
"line_items": [
{
"id": "466157049",
"quantity": "1"
},
{
"id": "23",
"quantity": "2"
}
]
}