Loading ...
Sorry, an error occurred while loading the content.

Using TaffyDB with Multiple JSON Nodes

Expand Messages
  • exuma_cam
    I am new to TaffyDB, so I apologize if this is a noob question. I am coming from a WebSQL framework. The data I am accessing from the web service are JSON
    Message 1 of 2 , Aug 29, 2012
    • 0 Attachment
      I am new to TaffyDB, so I apologize if this is a noob question. I am coming from a WebSQL framework. The data I am accessing from the web service are JSON stings, so it makes Taffy a no brainer.

      My problem occurs when I have to deal with complex JSON strings that contain multiple object nodes. For instance, I have a string that basically looks like this:

      {
      "id": "00241",
      "lastname": "Smith",
      "firstname": "Adam",
      "address1": "1 Main Street",
      "city": "New York",
      "state": "NY",
      "zip": "01001",
      "type": [
      {
      "id": "001",
      "desc": "New Customer"
      }
      ],
      "pets": [
      {
      "id": "001",
      "type": "dog",
      "breed": "Boxer"
      }
      ],
      "openinvoices": [
      {
      "id": "000127",
      "amount": "-147946",
      "duedate": "",
      "desc": "Payment Received"
      }
      ]
      }

      How does TaffyDB handle the nodes "type", "pets" and "openinvoices". Am I right in thinking that I've got to have four databases here? In "SQL Land" this is one database with four tables. Maybe I am just having trouble making the leap.

      Thanks,

      Cam (@camcollins)
    • exuma_cam
      I think I just answered my own question and the answer is no one database should suit me just fine. In playing with this it dawned on me that using standard
      Message 2 of 2 , Aug 29, 2012
      • 0 Attachment
        I think I just answered my own question and the answer is "no" one database should suit me just fine.

        In playing with this it dawned on me that using standard JSON to access an array within an object is the best way to do this. Let's say I load the JSON string in my original example into a TaffyDB and I wanted to update column "breed" to "Hound". Something like this worked for me. I just match up the JSON syntax:

        db().update({"pets":[{"breed":"Hound"}]});

        I did notice that the other name/value pairs in the array were removed, namely "id" and "type". I verified this both in the console and by loading the database into localStorage. Why is this the case for arrays "[]", but not regular objects.

        db().update({"lastname":"Collins"});

        Updates the column lastname and the other name value pairs remain unchanged (e.g. firstname: Adam). Is it because my sytax overwrites the entire array?

        --- In taffydb@yahoogroups.com, "exuma_cam" <camc@...> wrote:
        >
        > I am new to TaffyDB, so I apologize if this is a noob question. I am coming from a WebSQL framework. The data I am accessing from the web service are JSON stings, so it makes Taffy a no brainer.
        >
        > My problem occurs when I have to deal with complex JSON strings that contain multiple object nodes. For instance, I have a string that basically looks like this:
        >
        > {
        > "id": "00241",
        > "lastname": "Smith",
        > "firstname": "Adam",
        > "address1": "1 Main Street",
        > "city": "New York",
        > "state": "NY",
        > "zip": "01001",
        > "type": [
        > {
        > "id": "001",
        > "desc": "New Customer"
        > }
        > ],
        > "pets": [
        > {
        > "id": "001",
        > "type": "dog",
        > "breed": "Boxer"
        > }
        > ],
        > "openinvoices": [
        > {
        > "id": "000127",
        > "amount": "-147946",
        > "duedate": "",
        > "desc": "Payment Received"
        > }
        > ]
        > }
        >
        > How does TaffyDB handle the nodes "type", "pets" and "openinvoices". Am I right in thinking that I've got to have four databases here? In "SQL Land" this is one database with four tables. Maybe I am just having trouble making the leap.
        >
        > Thanks,
        >
        > Cam (@camcollins)
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.