0

I'm trying to parse a list into js.

I have tried a few workarounds but I can't seem to access the array to begin looping through.

def parse_list(request):

    first_list = [
        ['Harlem Babies Limited', 'Harlem Babies Limited', 52.0561, 1.154311],
        ['Guardians of the Galaxy Ltd',  'Guardians of the Galaxy Ltd',  51.8866354323239,  0.185223639282133],
        ['Marvel Limited', 'Marvel Limited',52.5653341835845, -0.242548375987545],
        ['Mantaray Marketing Limited',  'Mantaray Marketing Limited',  52.331251,  0.332465]
        ]

    second_list = json.dumps(first_list)

    return render(request,'temp.html', 'second_list':second_list)

Template
<html>
<script type="text/javascript"> 
var second_list = '{{second_list}}';
</script>
</html>

I expected the outcome to be...

second_list = [
    ['Harlem Babies Limited', 'Harlem Babies Limited', 52.0561, 1.154311],
    ['Guardians of the Galaxy Ltd',
  'Guardians of the Galaxy Ltd',
  51.8866354323239,
  0.185223639282133],
    ['Marvel Limited', 'Marvel Limited', 52.5653341835845, -0.242548375987545],
    ['Mantaray Marketing Limited',
  'Mantaray Marketing Limited',
  52.331251,
  0.332465]
]

But the actual output is...

second_list = "[[&quot;Harlem Babies Limited&quot;, &quot;Harlem Babies Limited&quot;, 52.0561, 1.154311], [&quot;Guardians of the Galaxy Ltd&quot;, &quot;Guardians of the Galaxy Ltd&quot;, 51.8866354323239, 0.185223639282133], [&quot;Marvel Limited&quot;, &quot;Marvel Limited&quot;, 52.5653341835845, -0.242548375987545], [&quot;Mantaray Marketing Limited&quot;, &quot;Mantaray Marketing Limited&quot;, 52.331251, 0.332465]]"
2
  • 1
    What happens when you remove the ' from the javascript wrapping the mustache braces? Commented Jul 23, 2019 at 15:57
  • 1
    Have you tried {{second_list | safe}}? Commented Jul 23, 2019 at 15:58

1 Answer 1

2

I'm assuming you're using django.

You need to pass the json through as is without html escaping.
You can do this by using the safe filter:

<script type="text/javascript"> 
var second_list = {{second_list|safe}};
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

This does change the parsed list into something that resembles an array but it's still a block of text. "[["Harlem Babies Limited", "Harlem Babies Limited", 52.0561, 1.154311], ["Guardians of the Galaxy Ltd", "Guardians of the Galaxy Ltd", 51.8866354323239, 0.185223639282133], ["Marvel Limited", "Marvel Limited", 52.5653341835845, -0.242548375987545], ["Mantaray Marketing Limited", "Mantaray Marketing Limited", 52.331251, 0.332465]]"
Do want to get an array in your javascript file?
@BobbyStearman did you remove the single quotes like in my example? If you remove them it should be an object as expected. :)
@Turtlefight Thanks, No I didn't initially as it Sublime highlights it as a syntax error i.e. the last curly bracket is highlighted red. Everything seems to work now!

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.