Brecht Valcke Brecht Valcke - 8 days ago 6
JSON Question

Fitting json data in a single query string variable

I am writing a web application to exchange contact information fast via QR.
I use a QR api wich is formatted like this:

`http://api.qrserver.com/v1/create-qr-code/?data=MyData&size=400x400`


I have json data formatted in a string, example of output:

`http://[myapp-url]/RecieveContact.html?Name=John%20Diggle&Title=IT%20Consultant&Organisation=testcomp&Telwork=0498553311&Telhome=&Gsm=0498553311&Email=testemail@mail.be&Website=www.testwebsite.be&Birthdate=24/04/97&Addresswork=&Addresshome=`


JSON data:

{"Name":"John Diggle",
"Title":"IT Consultant",
"Organisation":"testcomp",
"Telwork":"0498818587",
"Telhome":"",
"Gsm":"0498818587",
"Email":"testemail@mail.be",
"Website":"www.testwebsite.be",
"Birthdate":"24/04/97",
"Addresswork":"",
"Addresshome":""}


The problem is when you put this url in the QR generator it only recognises the
Name
parameter. I understand why this happens.

The question is is there a way using javascript to convert all this data in a string and convert it back on the recieving end?

Or does anyone know another potential fix for this problem?

Answer

You need to URL encode data with special characters you put into a URL:

var url = 'http://[myapp-url]/RecieveContact.html?Name=John%20Diggle&Title=IT%20Consultant&Organisation=testcomp&Telwork=0498553311&Telhome=&Gsm=0498553311&Email=testemail@mail.be&Website=www.testwebsite.be&Birthdate=24/04/97&Addresswork=&Addresshome=';

var query = 'http://.../?data=' + encodeURIComponent(url) + '&size=400x400';

This way you can represent characters like & inside a query string.