vishnu
vishnu

Reputation: 4599

Extract the JSON file using angularjs

I have the following valid JSON in the following way,

{
  "cancelled": false,
  "text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<PrintLetterBarcodeData uid=\"345135084445\" name=\"Venkatasiva M\" gender=\"M\" yob=\"1985\" co=\"S/O Narayana\" vtc=\"Pamidi vari palem\" dist=\"Guntur\" state=\"Andhra Pradesh\" pc=\"522112\"/>",
  "format": "QR_CODE"
}

How do i extract the uid and personal details(name, gender and yob) from the JSON file.

Upvotes: 0

Views: 503

Answers (2)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

I would recommend to convert your JSON file text and that is possible by below sort way:

//Your JSON file data...
var returnJSONFile = {
  "cancelled": false,
  "text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<PrintLetterBarcodeData uid=\"345135084445\" name=\"Venkatasiva M\" gender=\"M\" yob=\"1985\" co=\"S/O Narayana\" vtc=\"Pamidi vari palem\" dist=\"Guntur\" state=\"Andhra Pradesh\" pc=\"522112\"/>",
  "format": "QR_CODE"
};

//Object Initialization...
var x2js = new X2JS();

//Convert XML to JSON...
var xml2Json = JSON.stringify(x2js.xml_str2json(returnJSONFile.text));
alert(xml2Json);

Please check my JSFIDDLE for more understanding.

I accomplish this by using x2js library.

This library provides XML to JSON (JavaScript Objects) and vice versa javascript conversion functions. The library is very small and doesn't require any other additional libraries.

API functions

  1. new X2JS() - to create your instance to access all library functionality. Also you could specify optional configuration options here
  2. X2JS.xml2json - Convert XML specified as DOM Object to JSON
  3. X2JS.json2xml - Convert JSON to XML DOM Object
  4. X2JS.xml_str2json - Convert XML specified as string to JSON
  5. X2JS.json2xml_str - Convert JSON to XML string

Hope this help you well!

Upvotes: 1

Umair Hamid
Umair Hamid

Reputation: 3557

There are many ways, but in a simple way you can get text (yours xml) from json. Then you pass this to the following function which will return you json of input XML.

// Changes XML to JSON
function xmlToJson(xml) {

    // Create the return object
    var obj = {};

    if (xml.nodeType == 1) { // element
        // do attributes
        if (xml.attributes.length > 0) {
        obj["@attributes"] = {};
            for (var j = 0; j < xml.attributes.length; j++) {
                var attribute = xml.attributes.item(j);
                obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
            }
        }
    } else if (xml.nodeType == 3) { // text
        obj = xml.nodeValue;
    }

    // do children
    if (xml.hasChildNodes()) {
        for(var i = 0; i < xml.childNodes.length; i++) {
            var item = xml.childNodes.item(i);
            var nodeName = item.nodeName;
            if (typeof(obj[nodeName]) == "undefined") {
                obj[nodeName] = xmlToJson(item);
            } else {
                if (typeof(obj[nodeName].push) == "undefined") {
                    var old = obj[nodeName];
                    obj[nodeName] = [];
                    obj[nodeName].push(old);
                }
                obj[nodeName].push(xmlToJson(item));
            }
        }
    }
    return obj;
};

The function will return you json so you can easily extract desired node.

Upvotes: 0

Related Questions