Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
making a json object in php that is parsable by javascript

I have a huge list of things with each of those things broken down into four sub-things. I am easily able to gather them in nested arrays in PHP, but when I use json_encode to pass them as a JSON object to my javascript client side, the JSON.parse() function always fails because my data is filled with all kinds of entities like ' / \ " & % {} []

It's a shame there is no constant that you can add to json_encode to ensure it's javascript ready.

What characters must I escape or otherwise deal with to ensure I get a javascript parsable object. I was unable to find one. I could easily write a function to handle this if I knew which characters to deal with.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There should be no problem with your approach. Maybe there is a problem when rendering the data? Did you try the following to output the json?

echo 'var myjs = ' . htmlspecialchars(json_encode($obj)) . ';';

If you render the json in a different url/endpoint you do not have to encode anything yourself. However you should make sure your http headers are set correctly

header('Content-Type: application/json');
echo json_encode($obj);
die(0); # thx for requesting my endpoint
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!