0

I generated a string that contains a json in my C# codebehind, then I send it to the frontend to use in Javascript. I want to use that json inside a function like $.getJSON("<%=myJson%>") but I'm getting some errors

I already tried to use the string directly since json is javascript code but it doesn't work. The console shows

"Uncaught SyntaxError: missing ) after argument list"

Then I tried JSON.parse(myJson) but it´s also not working. The console shows

"Uncaught SyntaxError: Unexpected identifier"

This is how my Json string looks in codebehind:

"\"iss\":\"123123123\",\"exp\":123123,\"jti\":\"asdasdasdasd\",\"sub\":\"asdasdasdasd\",\"grants\":{\"identity\":\"John\",\"voice\":{\"incoming\":{\"allow\":true},\"outgoing\":{\"application_sid\":\"asdasdasdasd\"}}}}"

Then when I send it to Javascript in the frontend:

$.getJSON("
"iss":"123123123","exp":123123,"jti":"asdasdasdasd","sub":"asdasdasdasd","grants":{"identity":"John","voice":{"incoming":{"allow":true},"outgoing":{"application_sid":"asdasdasdasd"}}}}")

UPDATE I fixed the json syntax in codebehind:

"{\"iss\":\"123123123\",\"exp\":123123,\"jti\":\"asdasdasdasd\",\"sub\":\"asdasdasdasd\",\"grants\":{\"identity\":\"John\",\"voice\":{\"incoming\":{\"allow\":true},\"outgoing\":{\"application_sid\":\"asdasdasdasd\"}}}}"

now I'm getting this error in javascript:

$.getJSON('{"iss":"123123123","exp":123123,"jti":"asdasdasdasd","sub":"asdasdasdasd","grants":{"identity":"John","voice":{"incoming":{"allow":true},"outgoing":{"application_sid":"asdasdasdasd"}}}}')

GET http://localhost:.... 400 (Bad Request)

I think it's because this function expects a url to access but how can I make it read my json string?

asdf31
  • 178
  • 1
  • 12
  • Welcome to Stack Overflow. We can help you more quickly if you create a [mcve] that illustrates your problem. Note that this is NOT your entire project. It just needs to be a small example that you create from scratch that uses the concepts you are asking about. Be sure to read the link I gave for tips on how to create such an example. – Code-Apprentice Aug 28 '19 at 23:27
  • That's not a valid json string, here's a minimal example `{"key":"value"}` – Velimir Tchatchevsky Aug 28 '19 at 23:27
  • @VelimirTchatchevsky That's the problem he's trying to solve. – Barmar Aug 28 '19 at 23:29
  • @Barmar adding quotes around it doesn't make it valid json either,that's why I give an example of a simple json `object` – Velimir Tchatchevsky Aug 28 '19 at 23:30
  • Oh, I assumed he was using a C# library to create the JSON. – Barmar Aug 28 '19 at 23:31
  • How are you creating the JSON in the first place? Use a standard library function rather than coding it yourself, there are many details that are easy to get wrong. – Barmar Aug 28 '19 at 23:32
  • I got that json by decoding a token: `myJson = Jose.JWT.Decode(Token, secretKey);` – asdf31 Aug 29 '19 at 13:45

2 Answers2

1

Use single quotes around the JSON in the JavaScript code, since JSON uses double quotes for the embedded strings.

$.getJSON('<%= myJson %>');

You also need to fix the code that's creating myJson in the first place, so that it's correctly formatted as JSON. See How to create JSON string in C#

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

your json is wrong, you are missing a { at the beginning. The following function should work:

JSON.parse(`{"iss":"123123123","exp":123123,"jti":"asdasdasdasd","sub":"asdasdasdasd","grants":{"identity":"John","voice":{"incoming":{"allow":true},"outgoing":{"application_sid":"asdasdasdasd"}}}}`)
Teiem
  • 1,329
  • 15
  • 32