Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Representing matrix in JSON

Tags:

json

matrix

I am trying to have matrix style data in JSON, but it doesn't seem to work. Can anyone help me understand what am I doing wrong?

        {
   "took": 12,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 44,
      "max_score": 1,
      "hits": [
      {
            "_index": "transactions",
            "_type": "transaction",
            "_id": "trans0007",
            "_score": 1,
            "_source": {
                "fundRelation": "[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],
                                [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0],
                                [0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [1,0,0,1,0,0,1,0,0,1,0,1,0,1,1,1,0,1,0,0],
                                [0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
                                [0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0],
                                [0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0],
                                [0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0],
                                [1,0,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,1,1,0],
                                [1,0,0,1,0,0,1,0,1,1,0,1,0,1,0,1,0,1,1,0],
                                [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,0,0,1,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
                                [0,0,1,1,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0],
                                [0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0],
                                [0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0]
                            ",
                "fundName": ["Fund A","Fund B","Fund C","Fund D","Fund E","Fund F","Fund G","Fund H","Fund I","Fund J","Fund K","Fund L","Fund M","Fund N","Fund O","Fund P","Fund Q","Fund R","Fund S","Fund T"],
                "fundColor": ["#9ACD32","#377DB8","#F5DEB3","#EE82EE","#40E0D0","#FF6347","#D8BFD8","#D2B48C","#4682B4","#00FF7F","#FFFAFA","#708090","#708090","#6A5ACD","#87CEEB","#A0522D","#FFF5EE","#2E8B57","#F4A460","#FA8072"]
            }
         }     ]
   }
}

Not sure what i am doing wrong.

I am getting the following error message:

> Parse error on line 19: ...    "fundRelation": "[1,0,0,1,0,0,1,0,1,
> -----------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

at http://jsonlint.com/

like image 318
Lav Avatar asked Aug 22 '15 05:08

Lav


3 Answers

Here problem is, you are trying to assign multi line string value to fundRelation which is not valid JSON.

....
"fundRelation": "[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]",
...

Or you can do something like this :

{
    "took": 12,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 44,
        "max_score": 1,
        "hits": [
            {
                "_index": "transactions",
                "_type": "transaction",
                "_id": "trans0007",
                "_score": 1,
                "_source": {
                    "fundRelation": [
                        [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],
                        [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0]
                    ],
                    "fundName": ["Fund A","Fund B","Fund C","Fund D","Fund E","Fund F","Fund G","Fund H","Fund I","Fund J","Fund K","Fund L","Fund M","Fund N","Fund O","Fund P","Fund Q","Fund R","Fund S","Fund T"],
                    "fundColor":["#9ACD32","#377DB8","#F5DEB3","#EE82EE","#40E0D0","#FF6347","#D8BFD8","#D2B48C","#4682B4","#00FF7F","#FFFAFA","#708090","#708090","#6A5ACD","#87CEEB","#A0522D","#FFF5EE","#2E8B57","#F4A460","#FA8072"]
                }
            }
        ]
    }
}
like image 84
Darshan Patel Avatar answered Nov 15 '22 06:11

Darshan Patel


You can't have multi line strings in javascript (except in the newish engines using the ` operator). You need to escape the end of each line of fundRelation by adding a \ to end end of each line.

Alternatively, don't store the matrix data as a string. remove the quote at the beginning and end of the array and store it as a standard array

like image 26
Patrick Avatar answered Nov 15 '22 07:11

Patrick


Looks like it's not ok with the " splitting in multiple lines. In any case, shouldn't that be like this:

...
"fundRelation": [
    [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],
    [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    ...
    [0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0]
]
...

Note the extra enclosing brackets [...].

like image 29
Marin Avatar answered Nov 15 '22 06:11

Marin