1

I have a JavaScript script which receives information from a Python server. The Python server outputs a list, which is converted to a string prior to being sent to the JavaScript script.

I'd like to be able to convert the received string into a form that can be indexed with JavaScript. Here's an example output string from the Python server:

var Message = [['Word1A', 'Word1B'], ['Word2A', 'Word2B'], ['Word3A', 'Word3B']];

Given the above example, I'd like to be able to query the received string as as an indexed array:

var x;
for (x in Message) {
    alert(x[0]};

The above example should return:

Word1A
Word2A
Word3A

What's the best way to go about this?

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
jars121
  • 1,127
  • 2
  • 20
  • 35
  • Which part are you having trouble with? – juanchopanza Jun 27 '15 at 13:29
  • Which way you use for data transport? If it is the HTTP protocol and AJAX requests you can add content/type header like it is done here http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type. And on the client side it will be parsed automatically to JavaScript list or you can insert string into template as JS list. – ErDmKo Jun 27 '15 at 13:41
  • @juanchopanza I'm struggling with how to convert the string into an indexed array. – jars121 Jun 27 '15 at 23:56
  • @ErDmKo I'm using websockets, and am only able to emit strings (no lists/arrays). – jars121 Jun 27 '15 at 23:56

2 Answers2

2

You can simply use the json Python module

reply = json.dumps(Message)

to convert the array in a JSON formatted string.

Then on the client side you decode the JSON string with

message = JSON.parse(msg);

and you will get an array of 2-elements arrays

6502
  • 112,025
  • 15
  • 165
  • 265
  • I really like this approach, but I'm unable to make it work. Python produces a nested list (as per original post); do I need to do anything to it before using the json.dumps() method? – jars121 Jun 29 '15 at 01:20
0

This should work,

var Message = [['Word1A', 'Word1B'], ['Word2A', 'Word2B'], ['Word3A', 'Word3B']];
var x;
for (x in Message) {
    alert(Message[x][0]);
}

Editing my answer as per you comments below which says the input would be "['Word1A', 'Word1B'], ['Word2A', 'Word2B'], ['Word3A', 'Word3B']"

Solution being,

var Message = "['Word1A', 'Word1B'], ['Word2A', 'Word2B'], ['Word3A', 'Word3B']";
var array = Message.replace(/,|[|]/g, "").split(" ");
for(var x = 0; x < array.length -1; x=x+2){
 alert(array[x].replace("[","").replace("]",""));
}

I must add, this solution will work but I'm not that fluent with regex, so more optimal solution could be achieved. Hope it helps!

Sudhansu Choudhary
  • 3,322
  • 3
  • 19
  • 30
  • As Message is a string (not an array), this prints each individual letter to the alert console. This would be correct if Message had been converted into a JavaScript array prior to printing. – jars121 Jun 27 '15 at 23:51
  • so you mean the input is, `"['Word1A', 'Word1B'], ['Word2A', 'Word2B'], ['Word3A', 'Word3B']"`?? – Sudhansu Choudhary Jun 28 '15 at 00:02
  • Yes that's correct. The input is a list, converted to a string, from Python. – jars121 Jun 28 '15 at 00:03
  • Yes it did, to an extent. I've decided to change my approach for the time being though. Thanks again for all your help! – jars121 Jun 29 '15 at 21:17