0

I have this JSON encoded with PHP:

{
    "id": "37",
    "user_id": "1",
    "account": "ssdv",
    "amount": "5002",
    "subject": "\u0647\u062f\u06cc\u0647",
    "tags": "[{\"ttt\"}]"
}

the php array for above json was:

array (
  'id' => '37',
  'user_id' => '1',
  'account' => 'ssdv',
  'amount' => '5002',
  'subject' => 'هدیه',
  'tags' => '["ttt"]',
)

tags index is read from MySQL table with JSON datatype, so I can't change format of tags it have to be json array.

I've tried to decode first string as json by this code:

<script>
var obj = JSON.parse('{"id":"37","user_id":"1","account":"ssdv","amount":"5002","subject":"\u0647\u062f\u06cc\u0647","tags":"[{"ttt"}]"}');
</script>

but it throws this error:
Uncaught SyntaxError: Unexpected token t in JSON at position 86 at JSON.parse (<anonymous>)

how can i parse this json as a valid json in javascript?

update

my table is like this:

id    user_id    account    amount    subject    tags

37    1          ssdv       5002      هدیه        ["ttt"]

tags field is MySQL json type.

I fetch this record and try to json_encode it, the tags index turns to "tags":"[{"ttt"}]" after encode. but it cause error when i try to JSON.parse(myEncodedRecord). it looks the tags have to be like this "tags":["ttt"]. I know I can achieve this by preg_replace() , but is there any better way?

Mohammad Salehi
  • 565
  • 1
  • 12
  • 33
  • 3
    Why are you not using the string you have shown on top? That string is correctly encoded (assuming there is a closing `}`...) and you can send it directly to javascript without having to quote and parse it. – jeroen Sep 05 '18 at 13:43
  • Possible duplicate of [PHP decoding and encoding json with unicode characters](https://stackoverflow.com/questions/7381900/php-decoding-and-encoding-json-with-unicode-characters) – scrowler Sep 05 '18 at 13:46
  • 1
    You have embedded double-quote marks inside the tags parameter. You have to escape them. – mlewis54 Sep 05 '18 at 13:46
  • 1
    You can and should change the MySQL json format. if you run it through json_decode, you'll get a plain PHP array, you can then put that array in the 'tags' key and json_encode the whole thing and avoid any nasty issues with double encoding/decoding stuff. Let us know how that goes. – Royal Wares Sep 05 '18 at 13:47
  • 1
    The double quotes not being escaped is not the problem here, Alexander is right that the DB encoding needs to be set correctly before the query is run. – scrowler Sep 05 '18 at 13:49
  • 2
    Note you would not get `[{\"ttt\"}]` from `["ttt"]` when json encoding as that would be invalid syntax. It would instead be encoded like so `"tags":"[\"tt\"]"` – Patrick Evans Sep 05 '18 at 13:51
  • 1
    It looks like the tags property is malformed, the "tags": "[{"ttt"}]", it should look like "tags": [{"ttt": "some_value"}] or "tags": [{"some_property": "ttt"}] or if tags is meant to be a string then the characters inside must be escaped properly. – Harry Chilinguerian Sep 05 '18 at 13:52
  • The issue mentioned by @PatrickEvans must be the problem I believe. – Krishnadas PC Sep 05 '18 at 14:25

3 Answers3

1
JSON.parse('{"id":"37","user_id":"1","account":"ssdv","amount":"5002","subject":"\u0647\u062f\u06cc\u0647","tags":["ttt"]}');

If tags are array why you need {}. I removed them and I was able to parse. This worked for me in the console. It is safe to use parsing inside a try

if tags is a single entity like if we have 3 tags and we need to show only it's name we can simply put it as an array. only if we need to display multiple attributes we need to fetch it as an array of objects. in first situation it will be like

"tags":["tag1name", "tag2name","tag3name"]

if it has multiple attributes

"tags":[{"name":"tag1name","type":"type1"},{"name":"tag2name","type":"type2"},{"name":"tag3name","type":"type3"}]

You can validate your JSON syntax with the help of the below website

https://jsonformatter.curiousconcept.com/

Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
1

You have 2 issues in your JSON String:

1.) You cannot use array as a string. So please change the string "[{"ttt"}]" to [{"ttt"}].

2.) When you are initializing an object, you cannot specify an index alone. You should also specify the value. So in the above point notice the object initialization in the array. If you provide only one string it will consider that as the index not the value. This is javascript not PHP.

So you can either:

1.) Change the `[{"ttt"}]` to `[{"ttt":"test"}]`. That should work or
2.) Change the `[{"ttt"}]` to `[{"value":"ttt"}]` however you feel comfortable.

I have considered the first option so your result string is:

var txt = '{"id":"37","user_id":"1","account":"ssdv","amount":"5002","subject":"\u0647\u062f\u06cc\u0647","tags":[{"ttt":"test"}]}'

Try this out and let me know if you face any issues.

Hope This Helps.

Kishen Nagaraju
  • 2,062
  • 9
  • 17
0

to avoid this error I had to decode tags field value first, then encode the whole array

code:

//get the record
$record = $this->db->get_where('table', ['id' => $id])->row_array();

//decode tags value
$record['tags'] = json_decode($expense['tags']);

//encode and use
$record = json_encode($record);

now it's a standard JSON.

Mohammad Salehi
  • 565
  • 1
  • 12
  • 33