1

I'm having a killer time trying to create a JSON object to return to a jQuery request.

I'm trying to use the jQuery chained select module

and I'm trying to create the JSDN object using Perl's JSON module.

I have no idea what I'm doing wrong or how I can even debug it, about the best I can do is get a JS dialog box coming up with "A unknown error....".

This is all I was trying to do to create the JSON object, I thought this would be all that is needed, but it's not. Any ideas?

TIA

use JSON;
my %data = (1 => 'val1',
            2 => 'val2',
            3 => 'val3',
           );
my $json_text = to_json(\%data);
print $json_text;
1
  • So what of the answers suggested was the correct one? With this question there is much guessing involved. Maybe add more details. Commented Aug 30, 2021 at 6:31

3 Answers 3

5

Running the script gives:

{"1":"val1","3":"val3","2":"val2"}

… which is the expected output.

The problem almost certainly lies with whatever code you are using to get the JSON from your server to the client.

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

3 Comments

I'm just printing it out - I thought that was all that was needed? "print $json_text"
@Chris: The code snippet you provided in the question above is correct, and works properly. Try it again yourself in a standalone script and see. (So it must be something else in your script that is wrong.)
Where are you printing it to? What does the output look like?
1

Have the javascript output the response text before processing to a textarea so you can see if there's something failing in transport. Also, you're going to want to put something in front of the data, like "myVar= " so that the data is assigned to something on the other end.

1 Comment

Thanks Woolstar, I'll try to figure out how to do that (I know nothing about JQuery)
1

Looks like you already figured it out your issue, and if you were wondering why it wasn't working is because the to_json method is not exported from JSON.pm in the older versions of the JSON module. The latest version on CPAN (2.0) exports to_json into your namespace but versions ~1.5 don't and require the JSON::. Many repos still carry this older version, and if you are running on CentOS ~5.3 or an older version of Fedora Core you will have the older JSON.pm.

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.