SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: Search in JSON

  1. #1
    SitePoint Zealot contactnaveen's Avatar
    Join Date
    Oct 2004
    Location
    India
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search in JSON

    I would like to know is it possible to search JSON using RegEx.

    Any pointers would be quite helpful!

    Naveen.

  2. #2
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Searching in JSON using RegEx.... is very similar to searching in Array using RegEx...

  3. #3
    SitePoint Zealot contactnaveen's Avatar
    Join Date
    Oct 2004
    Location
    India
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx bollysite for ur reply.

    Could u please let me know is there any code available online. That would reduce my workload

  4. #4
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    828
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I had a same question in past and I have found following code and it worked well.

    http://www.codeproject.com/KB/scripting/UsingJSON.aspx

    Hope it helps...

  5. #5
    SitePoint Zealot contactnaveen's Avatar
    Join Date
    Oct 2004
    Location
    India
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SSJ:

    I've downloaded the file which is available in the codeprojects. But I cudnt find any JS that searches JSON

    Naveen.

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do you intend to search? by name, value, name + value, etc?


  7. #7
    SitePoint Zealot contactnaveen's Avatar
    Join Date
    Oct 2004
    Location
    India
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is my JSON:

    [{'cname':'naveen','cid','1000'},{'cname':'naresh','cid','2001'},{'cname':'peter','cid','1001'},{'cname':'john','cid','1002'}]

    If I key in "n" in the textbox, it should automatically dropdown with the name that starts with "n".

    I could do that by looping JSON, but it will become slow if the list grows on. So to avoid that I'm planning to do the search with the help of RegEx.

    Naveen

  8. #8
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think that will help. Unless you split each object into parts, using regex or indexOf won't help.

    If you were to split the above string into four seperate strings, you could then search and return back the valid values and parse them back into an object for processing.


  9. #9
    SitePoint Zealot contactnaveen's Avatar
    Join Date
    Oct 2004
    Location
    India
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    If you were to split the above string into four seperate strings, you could then search and return back the valid values and parse them back into an object for processing.
    Will it be a efficient process if my JSON has say 10,000 objects?

    Naveen

  10. #10
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unless someone can write a valid regex pattern that will return the related object if the cname value has the letter `n` in then I can't think of another way. The best way to test is to generate a file with the json results in and test it in many ways.

    1: Parse the whole JSON string and loop through each item.
    2: Split the JSON string into seperate items and indexOf or regex test the text.
    3: Regex rest or indexOf the JSON string and attempt to pull it apart using indexOf and Remove.

    How is this data being passed to the javascript? If your passing this data as part of an Ajax call, maybe you should do the processing on the server side?


  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did something once where I had a very large list of items that needed to be looped through, and found it's actually pretty fast. Delegating the task back to the serverside wasn't an option, as I wanted it to work offline and also just plain wanted it to be very fast, so no http request trips for me. However, if you want it to be faster, think about how a database does indexing.

    Now, I don't know all the specifics of how you need to be able to search, but if its really something so simple as "match the search term at the start of the cname string" then you can easily make an index.
    Given:
    Code:
    databaseRows = [
      {'cname':'naveen','cid','1000'}
     ,{'cname':'naresh','cid','2001'}
     ,{'cname':'pete r','cid','1001'}
     ,{'cname':'john','cid','1002'}
    ]
    A lookup index on the first character of the cname column could be
    Code:
    lookupIndex = {
      'j' : [3] // 3 for the 4th row, since the databaseRows array start at index 0
    , 'n' : [0, 1]
    , 'p' : [2]
    }
    Then you add some helper code
    Code:
    searchString = 'not slow';
    firstChar = searchString.substr(0, 1)
    if (lookupIndex[firstChar]) {
        rowsToLoop = lookupIndex[firstChar];
        for (i=0, max=rowsToLoop.length; i<max; ++i) {
            rowNum = rowsToLoop[i];
            row = databaseRows[rowNum];
            if (0 == row.cname.indexOf(searchString)) {
                // searchString was matched at the start of the string
            }
        }
    }
    Assuming most of your rows don't start with the same character, this will greatly reduce the number of loops javascript will need to do.

    I had just pregenerated this lookup index serverside and included it along with the "databaseRows", rather than having javascript generate the index onload. Theres a bit of overhead either way, because javascript needs to parse it into memory still, but that's pretty fast. This type of stuff gzips well too.

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A further optimization you could do if you only need to index on cname, is to deliver the database sorted on cname. This will allow you to minimize the size of the index.
    Code:
    // instead of
    databaseRows = [
      {'cname':'naveen','cid','1000'}
     ,{'cname':'naresh','cid','2001'}
     ,{'cname':'pete r','cid','1001'}
     ,{'cname':'john','cid','1002'}
    ]
    
    // deliver it sorted on cname
    databaseRows = [
      {'cname':'john','cid','1002'}
     ,{'cname':'naresh','cid','2001'}
     ,{'cname':'naveen','cid','1000'}
     ,{'cname':'pete r','cid','1001'}
    ];
    
    // now instead of your index looking like this on your huge database
    lookupIndex = {
      'j' : [3] 
    , 'n' : [0, 1, 5, 88, 1046, 3344, /* tons more here*/]
    , 'p' : [2]
    }
    
    // the index is just a range, specifying start index and length
    // so much less text to download and parse
    lookupIndex = {
      'j' : [0, 1] // theres 1 cname which begin with 'j', the list begins at index 0
    , 'n' : [1, 2]
    , 'p' : [3, 1]
    , 'r' : [4, 100] // theres 100 cnames which begin with 'r', the list begins at index 4
    , 's' : [105, 5] // theres 5 cnames which begin with 's', the list begins at index 105
    }

  13. #13
    SitePoint Zealot contactnaveen's Avatar
    Join Date
    Oct 2004
    Location
    India
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will follow the steps that u've mentioned and will do a testing with large chunk of JSON data!

    Quote Originally Posted by gRoberts View Post
    How is this data being passed to the javascript? If your passing this data as part of an Ajax call, maybe you should do the processing on the server side?
    Yes I'm the passing the data to the client side by making an Asynchronous call. I felt server side processing wouldn't be a good approach for this operation, because for each and every keypress I've to contact the server which will be very costly!!

    Naveen.

  14. #14
    SitePoint Zealot contactnaveen's Avatar
    Join Date
    Oct 2004
    Location
    India
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a LOT crmalibu for ur suggestion and the code.

    I will run through the code let u know the result..

    Naveen.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •