• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Parsing JSON to extract values ...

JSON (JavaScript Object Notation) is a light weight Data-Interchange in Text format completely Language independant. It is used as a alternative to using XML, though both are structured approaches to mark up data. JSON like XML, has a structure with nesting of Data Elements. Both are Text based and both use Unicode. But the subtle benefits are - JSON is more clean and a little less verbose. Each instance of a JSON document describes one object with nested objects, arrays, strings, numbers, boolean values, or null values. Application may prefer using JSON over a XML structure since it is a subset of Javascript and you can use JavaScript’s own compiler to do the parsing. Also, navigating an object synthesized from JSON is identical to navigating any JavaScript object. It’s far easier than navigating through the DOM tree.

An object, in JSON parlance, is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

VSTS is able to record and playback Ajax/JSON requests. However, there’s no built-in support for doing things like Validating the contents of a JSON Response or extracting values if needed.

Parsing JSON embedded in a XML DOM Tree structure


    <?xml version=”1.0″ encoding=”utf-8″?>
      <soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”    xmlns:xsd=”http://www.w3.org/2001/XMLSchema“>
        <soap:Header>
          <SessionInfoHeader xmlns=”http://<whatever_the_domain>.com/webservices/“>
             <SessionInfo>{”SessionInfo”: {”SetOnLogin”: “True”, “CompanyID”: “my_Company_ID”, “Company_Name”: “my_Company_Name”, “LanguageID”: “enu”, ”LanguageVersion”: “1″, “User_ID”: “MANAGER”,   “UserName”: “my_User_Name”, “UserEmail”: ”user1@test.com” } }
             </SessionInfo>
          </SessionInfoHeader>
        </soap:Header>
      <soap:Body>
        <LogInResponse xmlns=”http://<whatever_the_domain>.com/webservices/” />
      </soap:Body>
    </soap:Envelope>


In the above XML DOM structure, the this is the JSON text.

Inside the Extract method, read the innertext property for the XML Node that contains the JSON instance, load the text to an XML Document Object, and then further using XPATH expression to reach the particular *Keys* , parse out the values for the same.

You can further learn about writing custom extraction Rules

public override void Extract(object sender, ExtractionEventArgs e)

        {

           if (e.Response.HtmlDocument != null)

            {

                if (e.Response.IsXml )

                {

                    myDoc = e.Response.XmlDocument;

                    XmlNodeList myXMLNodeList = myDoc.GetElementsByTagName(jsonTextVal);

                    foreach (XmlNode myXMLNode in myXMLNodeList)

                    {

                        myXMLNodeInnerText = myXMLNode.InnerText;

                    }

                }

            }

            //Covert the Json Text into Byte Array

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

            jsonTextInByteArray = encoding.GetBytes(myXMLNodeInnerText);

          

            //Call to parse the JSON and convert that into a XML infoset

            myXMLDocForJasonString = parseJsonToXML(jsonTextInByteArray);

            e.WebTest.Context.Add(this.ContextParameterName, myXMLDocForJasonString.InnerXml);

          

            //using XPATH Expression to further extract Elements

            jsonTagsToExtractArray = JSONValueTagsList.Split((new Char [] {‘ ‘, ‘,’}));

            for (int i = 0; i < jsonTagsToExtractArray.Length; i++)

            {

                XmlElement root = myXMLDocForJasonString.DocumentElement;

                myxpathExpression = jsonTextVal+“/” + jsonTagsToExtractArray[i];

                elementList = root.SelectNodes(myxpathExpression);

                foreach (XmlNode myXMLNode in elementList)

                {

                    e.WebTest.Context.Add(jsonTagsToExtractArray[i], myXMLNode.InnerText);

                }

 

            }

        }

        public XmlDocument  parseJsonToXML(Byte[] jsonTextInByteArray_param)

        {

            XmlDocument myXMLDocument = new XmlDocument();

            XmlDictionaryReader xmlReader = JsonReaderWriterFactory.CreateJsonReader(jsonTextInByteArray_param, XmlDictionaryReaderQuotas.Max);

            xmlReader.Read();

            String XMLString = xmlReader.ReadOuterXml();

            myXMLDocument.LoadXml(XMLString);

            return myXMLDocument;

        }

 Original Source:
http://msdnrss.thecoderblogs.com/2009/07/06/parsing-json-to-extract-values-for-further-parameterization-correlation/

AddThis Social Bookmark Button

Posted at 08:57:31 am | Permalink | Posted in JSON  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

e-messenger

MSN Web Messenger

eBuddy

ASP.NET Ajax CalendarExtender and Validation

AIM Express

Ajax Tools for ASP.NET Developers



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (176)
  • Ajax (112)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (84)
  • Email (23)
  • Facebook (84)
  • Flash (20)
  • Google (54)
  • Html (29)
  • Image (12)
  • International Calls & VOIP (7)
  • Java (58)
  • Javascript (280)
  • jQuery (200)
  • JSON (75)
  • Perl (2)
  • PHP (172)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (32)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (227)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (29)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange