extracting array of objects from xml response to js - apigee

so I'm extracting variables from my xml response and trying to reformat the objects but I want to do that the most efficient way possible. so I want to load the xml array of same objects into a js array that I can cycle thru and output the new format. I found a reference to type="nodeset" when extracting the XPath but i could not find a reference to it on the documentation.
what is the best way to load the full xml objects into a js variable and cycle thru the objects and output the new format
Thanks for any help you can give me on this.

Best way to accomplish this is with the XMLToJSON policy, a JavaScript callout in which you can mediate your payload, and then transform back with JSONToXML if you need it.

For an XML array that doesn't need filtering, you can use XPATH with type="nodeset", just as you described. This allows you to pull a node and all child nodes in a particular XPATH. As I'm sure you noticed, you can't do this by just extracting as type="string". Just know that you will need to convert the extracted variable to string before you can use the XML nodes like you do every other string. You can then do JSON.parse to take the string and manipulate the object like an array. The string conversion is as simple as calling a JS callout with the following code (if someone else has a better way, I'm all ears):
var extractedNodeSet = context.getVariable("extractedNodeSet");
var extractedNodeSetString = String(extractedNodeSet);
For an XML array that needs filtering/manipulation, I recommend to use XSLT along with the trusted <xsl:for-each select=...> element. This will let you set conditions on the XML array nodes, manipulate tags/data, and extract the data, all in one step. The only concern is that this isn't a JS array, so if you absolutely must have a JS array, then you'll need to then do an XMLtoJSON and work with the data from there.

Related

Parse JSON inside JSON attribute using JSONPath

I have a JSON list where one of the attributes of each element happens to be a JSON itself. It comes from a poor design upfront, but here it is.
I want to query the distinct attributes inside the JSON string contained in the elements.
Here is an example, just one item. I hand-wrote the code, but believe me that is valid JSON in production by the way it's generated
[{
"extraData": "{\"foo\":\"bar\"}"
}]
I would like to query for something like $.*.extraData.foo, but obviously such syntax does not work.
I am using IntelliJ IDEA Jsonpath evaluator.
The syntax should be something like parse($.*.extraData).*.foo
This article suggests me that no such operator is available to parse a JSON inside a JSON
It has to be JSONPath only for data analysis purposes. In Java, I use Jackson to parse the extraData object as a JsonNode, but my goal is to explore the large data set, and perhaps obtain some distinct values I want to use for enumeration purposes.
To JSON Path, the embedded JSON is just a string. The best you could do with a single operation is use a RegEx in the expression selector, but getting RegEx to identify JSON is really tricky, and that's if your implementation even supports RegEx.
I think the best option would be to use two paths:
The first path gets the embedded JSON. $.*.extraData
Parse that value
The second part gets the data you need. $.foo
This requires some extra code, but I think it's your only realistic option.

Is it possible to convert a result set from WebMatrix.Database.Query() to an XML string?

I'd like to consume the result set on the client side using a jQuery script, however I'm uncertain how to convert it to XML, other than building the XML DOM string manually, which I'd prefer to avoid. Is there an automatic way to convert it or obtain it as an XML string?
There are a number of APIs for generating XML, including Linq TO XML. However, I would serialize your server side data to JSON if you want to make it available to jQuery. The JSON helper is perfect for this.

Questions on passing and formatting JSON to HttpResponse

I just wanted to clarify a few questions I have. I'm building a JSON string and returning it using context.response. I'm just calling a url to an .ashx handler and trying to return JSON.
Here's how I have approached it so far:
List products = GetCarolProducts();
List images = new List();
foreach(Product p in products)
{
string imageTag = string.Format(#"<img src=""{0}"" alt=""""></li>", Util.ImageUrl(p.Image, false));
images.Add(imageTag);
i++;
}
string jsonString = images.ToJSON();
context.Response.Write(jsonString);
I'm using the example helper method here for the JSON: http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx
So my questions are:
1) I didn't want to pass back the entire product object. In fact I didn't want to pass that back at all. I wanted to pass back a bunch of image tags that I made in my foreach loop. so what I did was just create that string in each iteration of product and added it to a new generic list of type string. I assume this is fine, and that passing a generic string list can be serialized into JSON...that string is an object itself so this should be fine? thing is, it's not going to have any properties so I don't know. I guess I would be leary if this is going to work as if I were to pass an object such as product for example, it at least has properties in it such as ID, Name, etc.
So if this is not going to work, I am wondering how I can just receive back a list of JSON that gives me . I believe though I still need to be able to access that returned JSON with key/value though. But in terms of data, that's all I need in my jQuery, just a bunch of images.
So therefore my question is, must I pass an object that has properties in it so that my returned JSON has some params I can picked up via the jQuery since jQuery creates a nice typed object for you based on those properties? I don't think sending a generic string list would work in my situation then.
so not quite sure how to set this up and pass only the list of images to my ToJSON helper method and ultimately to my jQuery JSON parsing.
2) Not sure if I need to do anything special to tell the context.response that it's JSON other than pass it the final string that the helper method here creates? with XML you'd have to tell the response to format it as XML with context .Response .ContentType = "text/xml". So do I need to specify anything if I'm passing back JSON strings?
For the response content-type, look at Douglas Crockford's JSONRequest proposal.
Content-Type: application/jsonrequest

Does a "tag search" method exist working with RDF/XML documents in Flex?

I'm working on a RDF file in my application. I loaded it with the URLLoader and I've imported flash.xml.* class in order to manipulate the content of my RDF file. I need to count certain tags ("Description") in an efficient way. The number of the tags I'm searching for is variable and now I'm using a combination of methods firstChild, lastChild, nextSibling in while cicles in order to take the informations I need from the document. My question is if there's a more direct and general "search method" that helps me avoid that complex and not elegant search that I made.
The syntax #Property is used to directly access a Tag's property, right? Do you think that it could be useful for me to solve my problem?
Thank you all for the help
David
Sure. Try this:
// assuming xml is the XML object containing your document
var numberOfDescriptionElements:int = xml..Description.length();
In general, you should check out the details of E4X (ECMAScript for XML), a very handy and consise sub-language of ActionScript to access XML structures.

What's a good way of deserializing data into mock objects?

I'm writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I'd prefer not to generate the data in code like this:
var mockData = new Array();
mockData.push(new Foo(1, "abc", "xyz"));
mockData.push(new Foo(2, "def", "xyz"));
...
Rather I'd like to store the data in a file in some format that it can be easily serialized into my strongly-typed value objects (i.e. Foo above). Ideally I'd like to create the data in a self-describing format (i.e. what data type each field is, what class it represents, etc)
Does this make sense? Any suggestions?
I would highly recommend the asx3m library. It easily allows serialization to a very readable XML format like this for an object of class Foo:
<com.example.Foo>
<myVar>Something</myVar>
<myArrList>
<string>one</string>
<string>two</string>
</myArrList>
</com.example.Foo>
The code to de-serialize looks like this:
Asx3mer.instance.fromXML(someXMLObj)
The project site has some good examples and it's not too hard to get this off the ground.
Write a method to serialize an "inflated" version of your object. Put the output of that into a file and load it up as part of your test setup. When you want to edit the values, simply edit the xml file. I dont know if this is possible in flex but I will usually include these files as a resource in my test library so that I do not need to copy the file to any specific location for a test run.

Resources