Mohammad Salehi
Mohammad Salehi

Reputation: 776

json array encode in php and decode in javascript

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?

Upvotes: 0

Views: 3310

Answers (3)

Mohammad Salehi
Mohammad Salehi

Reputation: 776

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.

Upvotes: 0

Krishnadas PC
Krishnadas PC

Reputation: 6539

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/

Upvotes: 1

Kishen Nagaraju
Kishen Nagaraju

Reputation: 2200

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.

Upvotes: 1

Related Questions