QBO3 XML to JSON converter - json

I would like to convert XML data to a JSON object within QBO3.
Is there a native method available for this?

Try running the following in console:
var xml = "<head><node1>node1</node1><node2>node2</node2></head>"
var xmlToJson = new XMLtoJSON;
xmlToJson.fromStr(xml);
Note that in addition to the fromStr[ing] method, the XMLtoJSON object also has a fromFile method available.

Related

selecting a key-value from API response

I have this response from an API:
payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'}
how can i select the pix_expiration_date key??? I have tried:
payment_object['acquirer_response']['pix_expiration_date']
it doesnt work and it returns to me:
TypeError: string indices must be integers
Since you’re looking for json
Here’s a code you can start with, keep the good work :)
import json
#importing json
payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'}
###
dic=json.loads(payment_object["acquirer_response"])
#loading the object
dic['pix_expiration_date']
#it will return 2022-04-28T13:46:01.000Z
Are you the owner of the api? If yes:
Edit the response, because it’s sending an string not an object/dictionary
If No:
Convert the string to an dictionary, like if you using python try json library
Your json is not valid. I don't know what language are you using, this code works for javascript, you can easily translate it to another language
var payment_object = {'acquirer_response': '{"object":"transaction", "pix_expiration_date":"2022-04-28T13:46:01.000Z"}'};
var s=JSON.stringify(payment_object).replaceAll("\\","").replaceAll("\"{","{").replaceAll("}\"","}");
var paymentObject=JSON.parse(s);
console.log(paymentObject['acquirer_response']['pix_expiration_date']); // 2022-04-28T13:46:01.000Z

Can you use 'require' in react to import a library?

In my react project, I'm trying to convert XML data from an API call into JSON (using a library called xml-js).
As per the documentation, I'm importing the library in my parent component as follows
const convert = require('xml-js')
and then attempting the convert the API data as follows
const beerList =
'<Product>
<Name>Island Life IPA</Name>
<Volume>300ml/473ml</Volume>
<Price>$10/$13</Price>
<ABV>6.3%</ABV>
<Handpump>No</Handpump>
<Brewery>Eddyline</Brewery>
<IBU/>
<ABV>6.3%</ABV>
<Image>islandlife.png</Image>
<Country>New Zealand</Country>
<Description>Fruited IPA</Description>
<Pouring>Next</Pouring>
<IBU/>
<TapBadge/>
<Comments/>
</Product>'
const beerJs = convert(beerList,{compact: true, spaces: 4})
The errors are telling me that 'convert' is not a function, which tells me that the library isn't being imported. So is the issue with using 'require' syntax, and if so, what alternative would work in react?
which tells me that the library isn't imported
No. If that were the case, you wouldn't even get that far, your require call would throw an error.
Instead, it tells you that convert is not a function - which it isn't! Look at it in a debugger or log it, and you'll see it's an object with several functions inside. You can't call an object like a function.
Take a look at the xml-js docs again:
This library provides 4 functions: js2xml(), json2xml(), xml2js(), and xml2json(). Here are the usages for each one (see more details in the following sections):
var convert = require('xml-js');
result = convert.js2xml(js, options); // to convert javascript object to xml text
result = convert.json2xml(json, options); // to convert json text to xml text
result = convert.xml2js(xml, options); // to convert xml text to javascript object
result = convert.xml2json(xml, options); // to convert xml text to json text
So the solution is to call convert.xml2json and not convert:
const beerJs = convert.xml2json(beerList, {compact: true, spaces: 4})
Or maybe you want an actual object and not a JSON string, then you'd use convert.xml2js (in which case the spaces option is useless):
const beerJs = convert.xml2js(beerList, {compact: true})

JSON Parse using C#

Anybody has any idea how to parse below type of JSON object where same object named objects are present in master object. Below is an example
abc{
"abc":"123"
}
How to parse the child abc value?
You can use Json.NET it is vary easy to use. Json.NET handles JSON arrays natively, and will parse them into any type, string,int etc.
Are you currently using a JSON framework? Maybe check out Json.NET: http://www.newtonsoft.com/json.
Then you could do something like:
int onetwothree = jObject.SelectToken("abc.abc");
You can use http://www.newtonsoft.com/json to parse any json string on c#:
Your example :
var jsonString= "{\"abc\":{ \"abc\":\"123\" }}";
var jObj = JObject.Parse(jsonString);
var abcValue = jObj.SelectToken("abc.abc").Value<string>();

Json Parsing in Mirth OR Json in Mirth OR HL7 to JSON in Mirth

I want to use JSON as input of mirth channel and output like details Save in db or Create HL7 message.
In short Input as JSON Parse it and output as any format.
var object = {};
//Create JSON Object from HL7 Message.
object.mrn = msg['PID']['PID.3']['PID.3.1'].toString();
object.firstName = msg['PID']['PID.5']['PID.5.2'].toString();
object.lastName = msg['PID']['PID.5']['PID.5.1'].toString();
object.dob = msg['PID']['PID.7']['PID.7.1'].toString();
object.ssn = msg['PID']['PID.19']['PID.19.1'].toString();
//Create string from JSON Object.
var objjson = JSON.stringify(object);
logger.info(objjson);
//Create Json Object From JSON string.
var tt = JSON.parse(objjson);
Output
{"mrn":"1001","firstName":"COLLEEN","lastName":"OHALLAHAN","dob":"19850704","ssn":"123456789"}
HL7Message Sample
MSH|^~\&|ADT1|SHM|SHMADT|SHM|200812091126|SECURITY|ADT^A01^ADT_A01|MSG00001|P|2.5|
EVN|A01|200812091126||
PID|1|1001|1001^5^M11^ADT1^MR^SHM||OHALLAHAN^COLLEEN^^||19850704|F||2106-3|1200 N ELM STREET^^NEWPORT BEACH^CA^92660-1020^US^H|OC|(949) 555-1234|(949) 555-5678||S||PATID1001^2^M10^ADT1^AN^A|123456789|U1234567^CA|
NK1|1|OHALLAHAN^BRITTANY^M|SIS^SISTER||||N^NEXT-OF-KIN
PV1|1|I|2000^2012^01||||001122^ZOIDBERG^JOHN^|||SUR||||1|A0|
I was parsing through this page, and found your code Rikin patel. Actually when You create object and display it, it may appear in the console as a JSON data, when when you look your output it will be the normal XML driven format. But instead of the object when you use msg as below:
msg = JSON.stringify(object); //converting msg into JSON object
logger.info("json data:" + msg); //displaying the JSOn message
You will find the data being modified in the output.
As Per #Debugger, If some one want json file as input/Source then try this solution.
Mirth channel
Inbound Datatype as delimited text
Outbound DataType as Javascript
Make JavaScript Type of Destination and Write below code in Transformer:
//Create Json Object From JSON string.
var objJson = JSON.parse(messageObject.getRawData());
logger.info(objJson.propertyName);
Input:
{"mrn":"1001","firstName":"COLLEEN","lastName":"OHALLAHAN","dob":"19850704","ssn":"123456789"}
Output:
logger.info(objJson.firstName);
COLLEEN
Note:
Use connectorMessage.getRawData() instead of messageObject.getRawData() for Mirth 3.0+ version.
To receive JSON as Input in mirth channel, set inbound datatype as delimited text and in channel pre processor create Json object from received message and return the json object.
use the json object to get the details and store in some variables and use DB writer to save in db.
To build hl7 message, mirth provides few functions such as createSegment(seg name, index) to easily build your own hl7 message.

how to get individual valued from the json array..?

I got json array as output. could anyone explain me how to get indivual value from it for eg( C:/). The following is the json array
{"Name":["C:\/","D:\/","E:\/","F:\/","G:\/","My Documents","Microsoft Outlook","Microsoft Outlook Express","Opera","Mozilla","Internet Explorer Settings","FireFox","Desktop","Registry","SystemState"],"path":["C:\/","D:\/","E:\/","F:\/","G:\/","\/\/\/My Documents\/\/\/","\/\/\/Microsoft Outlook\/\/\/","\/\/\/Microsoft Outlook Express\/\/\/","\/\/\/Opera\/\/\/","\/\/\/Mozilla\/\/\/","\/\/\/Internet Explorer Settings\/\/\/","\/\/\/FireFox\/\/\/","\/\/\/Desktop\/\/\/","\/\/\/Registry\/\/\/","\/\/\/SystemState\/\/\/"],"hasChild":["1","1","1","1","1","0","0","0","0","0","0","0","0","0","0"]}
Assuming your language is JavaScript, you first need to parse the JSON (below I use jQuery's $.parseJSON method) and then access the appropriate property and array position. To retreive "C:\", you would do this:
var sJSON = '{"Name":["C:\/","D:\/","E:\/","F:\/","G:\/","My Documents"]}';
var oJSON = $.parseJSON(sJSON);
alert(oJSON.Name[0]); //displays C:\
If you're using JavaScript, you can use an eval() statement in place of a $.parseJSON because JSON is executable in JavaScript, but it is safer to use a $.parseJSON.
More on JavaScript eval(): When is JavaScript's eval() not evil?
Use a JSON parsing library for your language.
In JavaScript, you can use JSON.parse. If your output is in the var output, you would do:
var obj = JSON.parse(output);
console.log(obj.path[0]);