Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:48:56+00:00 2026-06-05T15:48:56+00:00

I’m trying to send JSON-object form MongoDB to page with ejs template engine. Here’s

  • 0

I’m trying to send JSON-object form MongoDB to page with ejs template engine. Here’s the response form my DB (a JSON-object):

"suppliers": [
    {
        "lastname": "lastname1",
        "firstname": "firstname1",
        "middlename": "middlename1",
        "mobtel": "mobtel1",
        "worktel": "worktel1",
        "email": "email1"
    },
    {
        "lastname": "lastname2",
        "firstname": "firstname2",
        "middlename": "middlename2",
        "mobtel": "mobtel2",
        "worktel": "worktel2",
        "email": "email2"
    }
]

As I get i from DB as an object, I have to stringify it to str to send as a var for EJS Template engine:

var myJSONText = JSON.stringify(doc.suppliers);
profile["suppliers"] =myJSONText;
...
res.render('profile.ejs',{profile:profile});

On front-end I get a plain string with json-data.
I parse it via jQuery to an object:

var jsonObject = jQuery.parseJSON('<%= profile.suppliers %>');
for (var i=0; i<jsonObject.length; i++){
    //trying to show field 'name' for every supplier
    alert(jsonObject[i]['lastname']);
}

…and it doesnt work, but should to alert me twice. I’ve tried to inspect a plain-text JSON-string, comming to front-end, and found out such symbols:

[{&quot;lastname&quot;: &quot; ... 

It seems that jQuery.parseJSON is not able to create a valid object from such string. What I’m doing wrong? Thanks!

UPDATE: Well, I’ve turned my code into:

NodeJS code:

profile["suppliers"] =doc.suppliers;
res.render('profile.ejs', { profile:profile });

jQuery code:

for (var i=0; i<'<%= profile.suppliers %>'.length; i++){
//trying to show field 'name' for every supplier
alert('<%= profile.suppliers %>'[i]['lastname']);
}

and it gives me never-ending loop of undefined alerts.

UPDATE 2: I’ve updated my source code, so:

NodeJS:

var myJSONText = JSON.stringify(doc.suppliers);
profile["suppliers"] =myJSONText;
res.render('profile.ejs',{profile:profile});

Client:

var jsonObject = <%- profile.suppliers %>;
$.each(jsonObject, function(item) {
    alert(item.lastname);
});

Generated source code:

var jsonObject = [{"lastname":"lastname1","firstname":"firstname1","middlename":"middlename1","mobtel":"mobtel1","worktel":"worktel1","email":"email1"},{"lastname":"lastname2","firstname":"firstname2","middlename":"middlename2","mobtel":"mobtel2","worktel":"worktel2","email":"email2"}];

Seems to be ok, but still gives me undefined alerts. Hoever, it alerts only twice, that means that it parses JSON correctly and just cannot get access to lastname property?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:48:57+00:00Added an answer on June 5, 2026 at 3:48 pm

    As you correctly diagnosed, &quot;s are being inserted into your JSON. This renders it invalid, preventing it being decoded.

    This is because in ejs, <%= ... %> does html output escaping, hence the &quot;. You need to use <%- profile.suppliers %> to avoid this.

    There’s no need to use JSON.parse on the client: JSON is already valid javascript code, so can be inserted straight into the code, rather than being stored as a string literal.

    var jsonObject = <%- profile.suppliers %>;
    $.each(jsonObject, function() {
        alert(this.lastname);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.