hbi99 / defiant.js Goto Github PK
View Code? Open in Web Editor NEWhttp://defiantjs.com
License: GNU Affero General Public License v3.0
http://defiantjs.com
License: GNU Affero General Public License v3.0
I used defiant to do search on JSON s but didnt work for Internet explorer .
Defiant is not supported by IE ??
Consider...
{
"a": {
"b": ""
}
}
Execute a search using a path like "//a". Result...
[
{
"b": {}
}
]
Can the Template also be a JSON Object?
Greetings Hakan,
My current xpath query is like so
//*[contains(title,"century") or contains(title,"honour")]
If I run this in the online xpath evaluator, it highlights the correct records, but the returned object will return in the same order the JSON is in. So in the above example, I want "Sayings of the Century" to be returned as the first object and then "Sword of Honour" as the next object. Is this possible at all?
This expression:
//text()
doesn't select the text-node children of two of the three occurrences of the element "category" using the tool at: http://defiantjs.com/#xpath_evaluator
I tried defiant and the search with xpath works great, but when the data is huge it's not going all the way till the end, I think its getting broken somewhere inbetween, The data that I tried was of 500KB.
Hi,
Thanks for the excellent work! Go Sweden!
Im using v. 1.2.8 to create a webworker snapshot.
Completes in about 12 secs (its a 10MB JSON file), and performs great.
I then packed up the app with cordova (for iOS), and watched memory usage peak above 600MB when the lib attempts to return the snapshot from the webworker to the main thread.
(At least, thats where I think the problem occurs).
This makes iOS call the applicationDidReceiveMemoryWarning, and then the app crashes :-(
Works fine in the simulator (which is not limited on RAM).
Ideas?
Best,
j.
I get some javascript error in IE10 The same works fine in Google Chrome.
To reproduce the issue, you can use this fiddle: http://jsfiddle.net/xLBH8/2/
The expected result would be:
This is the error I get in IE:
As a help, here is the stack:
Version infos:
IE10 (10.0.9200.16750 & Windows 7 Service Pack 1 inside Virtualbox):
Chrome (32.0.1700.77 & Kubuntu 13.10):
Any help is highly appretiated.
I wonder if it is possible for you to implement the result output like the jq language is doing: https://jqplay.org/
they have a great way to form the return json value.
Consider...
{
"a": {
"b": []
}
}
Search using an xpath like "//a". Result...
[
{}
]
Enabling features such as;
var found = JSON.search( data, '//item' );
found[0].nodename
// item
found[0].parentNode
// the parent object to the found item
found[0].search('./*[id=1]');
// searches only among children of found item
If you look at the following web page and continuously highlight and unhighlight a link while the tree is loading, you will notice that the link stops highlighting or gets stuck highlighted for 0.5 to 2 seconds:
http://www.yitbosoft.com/orgs/delta_sigma_phi/Alpha_Eta
The data that is being snapshotted is:
http://www.yitbosoft.com/api/v2/orgs/delta_sigma_phi/Alpha_Eta
The XPath Evaluator doesn't work as it should in IE8+
Consider...
var obj = {
key: [
{
child_key: "value"
}
]
};
console.log( "Before: " + JSON.stringify( obj, null, 4 ) );
JSON.search( obj, "//key" );
console.log( "After: " + JSON.stringify( obj, null, 4 ) );
Output is...
Before: {
"key": [
{
"child_key": "value"
}
]
}
After: {
"key": {
"child_key": "value"
}
}
I've not looked within the library to see if there is a nice way to fix it since I can get away with only using the library for search to see if something exists on a cloned object then extract from the original using regular object manipulation. The root of the problem is that XML doesn't have the idea of an array, so that would need to be expressed somehow in the generated XML.
It would be very handy if it did work, though. As it currently exists, match results are often not useful.
Thanks for this great library. I'm trying to use
[position() >= 10 and not(position() > 20]
to paginate/limit the result.
What's the right syntax? Thanks!
I try the following xpath: "//book[2]/category" with the XPath Evaluator. And the first "category" element is incorrectly highlighted. The second "category" element should be hightlighted instead.
Cannot find this module on npm. Not node ready yet?
Trying to use the xpath selection to select the title of the first fiction book.
Is it doable?
//*[category="fiction" and .first()]/title
Thanks
Hi,
thanks for Defiant, has helped me a lot!
I have a IE 11 issue, i am getting this error in the console:
"Object doesn't support property or method 'selectNodes' "
"File node.select.js, Line 14, Column: 3"
I get the same error in IE when I paste the demo code from json-search.htm into my file. I tested the json-search.htm on IE 11 and can confirm it's working.
I am using the 1.0-version of Defiant. All works as it should on Chrome Mac/PC
So I guess, there is a conflict with some other javascript library in my project, It's a rather big project with a lot of jquery, backbone, underscore, require etc...
Is there a possible workaround or update for this?
Hi,
I tried to use this library to perform some extraction of nodes in my JSON array. However, the function JSON.search is crashing when it is trying to evaluate this line
JSON.search(x,"//*[title = 'this is "NOT" me']);
I tried using escape, double escape but it continue to fail. Is it a bug or a problem with my expression ?
Thks
Create some benchmarks and improve the speed of the search function
JSON.toXML(Defiant.node.toJSON(Defiant.xmlFromString('<data><product id="100">MacBook Pro</product><product id="32">Sony Vaio</product></data>')))
Notice that the id attributes are lost when converting back to XML for String | Number elements
Thank you for the great little library, downloaded it today. The Defiant.search method works excellent. But trying out your simple sample from the website on my local machine the following error is thrown when Defiant.render is invoked:
"Reference to undeclared namespace prefix: 'xsl'."
Line: 695, coming from line 61 (render method)
Occurs in IE8 (company's default browser). Copied the code from the website and included defiant.js. Anything missing? Any ideas?
I am trying to return the full object from a JSON snapshot search. For example:
function search1(searchterm) {
var searched2 = JSON.search( snapshot, '//attributes[contains(Country, "'+searchterm+'")]' );
console.log(searched2);
}
I am trying to get the FULL object from the search
[{
"label": "Jean Makoun",
"x": 430.8337097167969,
"y": 257.7498474121094,
"id": "321",
"attributes": {
"Eigenvector Centrality": "0.3346787587121599",
"Betweenness Centrality": "0.0017126023775967546",
"Appearances": "66",
"No": "11",
"Country": "Cameroon",
"Club Country": "France",
"Club": "Rennes",
"Weighted Degree": "23.0",
"Modularity Class": "17",
"Date of birth / Age": "29 May 1983 (aged 31)",
"Degree": "23",
"Position": "MF",
"Eccentricity": "5.0",
"Closeness Centrality": "0.3202614379084967"
},
"color": "rgb(67,132,229)",
"size": 11.333333015441895
}];
However, it only returns the "attributes" portion of the object. I want the whole object that contains that searched term inside the "attribute" portion. Here is the return...
Object {Eigenvector Centrality: "0.4349752953094465", Betweenness Centrality: "0.007469372100698354", Appearances: "56", No: "4", Country: "Japan"…}Appearances: "56"Betweenness Centrality: "0.007469372100698354"Closeness Centrality: "0.3309320126069338"Club: "Milan"Club Country: "Italy"Country: "Japan"Date of birth / Age: "13 June 1986 (aged 27)"Degree: "29"Eccentricity: "5.0"Eigenvector Centrality: "0.4349752953094465"Modularity Class: "27"No: "4"Position: "MF"Weighted Degree: "29.0"__proto__: Object
Hi,
First of all thanks for the great project. I use the XPath evaluator from it.
I have a problem. If a string contains ' or ", i cannot look for it. Please take a look at my small examples at http://jsfiddle.net/7kdDY/ and http://jsfiddle.net/h949a/
Is there a known way to look for strings containing ' or ", or it is a bug?
Thanks and kind regards,
Gábor Lipták
Hi,
I have been playing around with relatively large json object with multiple levels. in total probably there are 3000 leaf nodes in the json tree.
It takes roughly about 170-250 ms to find a node.
I am not sure where all this time is spent but was wondering if:
1 - It is possible to pre-parse or compile the JSON some how to make it more performant for multiple searches. So you would do something like var compiledJSON = JSON.compile(); var search1Result = compiledJSON.search();var search2Result = compiledJSON.search();
2 - I do not seem to be seeing a difference with //leaf as a search path, or providing the full path. I would have expected that providing the full path or narrowing down the search should make it faster, but it does not.
Thanks a lot
Charbel
I was trying to emulate explorer 9-10 from windows 10 / edge and i a m getting SCRIPT5009: 'ActiveXObject' is undefined
i am using 1.3.1v
is there anything i can do , or its the intended if tested on emulator?
hello
first thanks for the effort put in this project.
I am using PhantomJS and since XSLTProcessor is not supported the ie.polyfill.js can be improved to at least add a message like so
if (typeof (data.transformNode) != "undefined") {
var str = data.transformNode(this.xsldoc),
span = document.createElement('span');
span.innerHTML = str;
return span;
}
else {
span = document.createElement('span');
span.innerHTML = "XSLTProcessor transformNode not implemented";
return span;
}
i was trying to add support myself but looks like it needs more time if ever it was easy.
Thanks and thats all for now.
ehab
Currently, there is no way to change the prefix ('@') of the XML attributes during the JSON conversion.
Use the '@' is quite painless when we have to manipulate the corresponding JSON property in Javascript.
It would be great to add a setter for the prefix string.
Example:
{
"a-b": "c"
}
Hi,
I am trying to search json with alphanumeric keys. Its failing to search.
here is my json. I would really appreciate your help.
"sizes": [
{
"L": "20/18",
"M": "10/7",
"S": "30/26",
"2XL": "50/20",
"color": "MIX"
},
{
"L": "10/6",
"M": "10/6",
"S": "10/8",
"2XL": "20/18",
"color": "SR"
},
{
"L": "10/7",
"M": "30/26",
"S": "50/20",
"3XL": "10/6",
"color": "MIX"
},
{
"L": "10/6",
"M": "10/8",
"S": "20/18",
"XL": "10/7",
"color": "BL"
},
{
"L": "30/26",
"M": "50/20",
"S": "10/6",
"3XL": "10/6",
"color": "FGN"
}
]
Hi,
Thanks for the awesome work :)
I've encountered a weird behaviour on objects with value=0, which I believe is a bug:
a = {"obj": {"four": 4, "zero":0, "zerodotone":0.1}}
JSON.search(a, "//zero")
> []
Whereas it works fine for every other number:
a = {"obj": {"four": 4, "zero":0, "zerodotone":0.1}}
JSON.search(a, "//zerodotone")
> [0.1]
This is a bit confusing because a search on a non-existing key also returns [], making it just like if my "zero" object does not exist.
a = {"obj": {"four": 4, "zero":0, "zerodotone":0.1}}
JSON.search(a, "//zero")
> []
JSON.search(a, "//notexistingkey")
> []
It appears that //*[contains(key, "text")] is case sensitive only. I could apply ".toLowerCase()" to the search text, but is there a way to apply it to the key argument?
Perhaps for things that don't directly use the key or field name, you could make it its own function instead of using XPath and have an argument for case sensitive or not? Or perhaps add that in addition to the functionality that already exists?
Hi,
Is there any way to do less than / greater than comparisons on string values? E.g. (This doesn't return any results):
//book[isbn > "0-300-00000-0"]
Many thanks,
Toby.
I'm using DefiantJS in combination with the Wordpress API. The WP-API JSON output is different then the example in the XPath Evaluator. The posts itself doesn't have a name as identifier. You just use the index to loop through an array.
Example of 10 Wordpress posts > https://jsonblob.com/5551ff82e4b0b306c890a7d1
I need to query through a lot of posts and for example query based on a location. This is working with DefiantJS. But I can't get it working with your pagination example.
Retrieve posts by 2 locations:
//*[contains(locations,"saba") or contains(locations,"curacao")]/..
Can you please help me? Thanks!
Hi there,
Quick question, I want to search for multiple words separately. From the book example in the evaluator, if my search term is "word century" then I want the 1st and the 3rd book highlighted.
I tried using the below
//*[contains(title,"century sword")] <-- does not work
//*[contains(title,"century") or contains(title,"sword")] <-- highlights only 3rd book
//*[contains(title,"century") or contains(title,"word")] <-- highlights 1st & 3d
Not sure if something is wrong with my query string. Hopefully you can give me some pointers.
Hi there,
Couldn't find anything online, just putting it here for exposure.
JSON.search(snapshot,'//*[contains(title, "manage") and contains(title, "project")]')
JSON.search(snapshot,'//*[contains(title, "manage project")]')
The second query returns me 1 object while the first one doesn't for some unknown reason.
Check attached image, see title for returned object.
Hi,
How long does it take a snapshot for a JSON of 500KB ? It takes around 30 minutes for me.
ALso, the web worker thing does not seem to be working for me, as the UI is being blocked. Any ideas on this?
Thanks.
I'm building an application that allows users to build a "dictionary", and I'm having the data save into a single JS object currentDictionary with each entry as an object in an array words within currentDictionary. I have a simple search box set up that works perfectly right up until I add a new word, which changes the currentDictionary object by inserting the new word into the words array. After adding the new word, the search no longer works.
Here is the code that runs whenever the search is executed:
var searchResults = [];
if (document.getElementById("searchOptionWord").checked) {
var wordNameSearch = JSON.search(currentDictionary, '//words[contains(name, "' + search + '")]/name');
for (var i = 0; i < wordNameSearch.length; i++) {
searchResults.push(wordNameSearch[i]);
}
}
if (document.getElementById("searchOptionSimple").checked) {
var simpleDefinitionSearch = JSON.search(currentDictionary, '//words[contains(simpleDefinition, "' + search + '")]/name');
for (var i = 0; i < simpleDefinitionSearch.length; i++) {
if (searchResults.indexOf(simpleDefinitionSearch[i]) < 0) {
searchResults.push(simpleDefinitionSearch[i]);
}
}
}
if (document.getElementById("searchOptionLong").checked) {
var longDefinitionSearch = JSON.search(currentDictionary, '//words[contains(longDefinition, "' + search + '")]/name');
for (var i = 0; i < longDefinitionSearch.length; i++) {
if (searchResults.indexOf(longDefinitionSearch[i]) < 0) {
searchResults.push(longDefinitionSearch[i]);
}
}
}
It would make sense to me that JSON.search() should run anew on the target object each time it is executed, but since it fails after the target changes, it seems that this is not the case?
Is this a limitation of the JS Objects or of Defiant.js?
(tested in Chrome 46.0.2490.80 m and Firefox 40.0.3)
EDIT: Interestingly enough, I've found that if I remove a word instead of adding one to the words array, the search still works. I don't know what this necessarily means, but it does seem strange.
Hello, not sure what the deal is, but right now bower is pulling down 1.2.6 as the latest (should be 1.3.1), and the "-latest" file is 1.2.5 (https://github.com/hbi99/defiant.js/blob/master/dist/defiant-latest.js) while defiant.js is 1.3.1 (https://github.com/hbi99/defiant.js/blob/master/dist/defiant.js)
I have spotted something strange when trying to connect multiple conditions with OR operator.
Demonstration of the issue:
XPATH EVALUATOR with default data:
//*[contains(title, "The") or contains(author, "Evelyn")]
Returns only 2 items - those which have the word "the" in their title.
//*[contains(title, "The") or contains(author, "velyn")]
Surprisingly it works when I delete the first character from the author condition.
All 3 items are returned in this case.
Must be some kind of problem with the regex replace. I will try to locate and fix it.
For eg.
JSON.search(snapshot, "//article_id | //title")
returns array like ["123","article1 header", "345" ," article 2 header", ...],
I tried using concat so I could return both values as one string and work from there but that fails.
Example xpath expression:
//*[not(contains(title, 'rings'))]/title
Hi,
thank you for this great plugin.
It would save me a lot of time if it would support IE 8. At the moment it isn't working in this browser. I don't know if you know it already.
An answer would be great!
Hi, Your project looks really interesting. Just to let you know I was trying out the xpath evaluator on http://defiantjs.com/#xpath_evaluator and found that the highlighting was wrong for xpaths that I cooked up myself, for example //book/category seems to only highlight 2 of the 4 books, although I can see they all have a category property. I did try the expression in the console and it worked correctly, so I suppose it is just a highlighting problem.
Nice work!
Cheers,
Peter
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.