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?

8
  • 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. Commented Sep 5, 2018 at 13:43
  • 1
    You have embedded double-quote marks inside the tags parameter. You have to escape them. Commented Sep 5, 2018 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. Commented Sep 5, 2018 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. Commented Sep 5, 2018 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\"]" Commented Sep 5, 2018 at 13:51

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

3 Comments

is preg_replace the best way to achieve this?
other than your output in php. Please show the php code. There must be something wrong at the server side I believe.
I update the question, I simply use a select query to get the record
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.