1

I am using flask and from my understanding JSON format consists of two types which are arrays and objects. After converting python variable into something like:

data = [{key:value}]

why do I always have to

return jsonify(data)

in order to send it back to js?

2
  • Because your first data is not JSON? Commented May 20, 2019 at 7:27
  • "from my understanding JSON format consists of two types" JSON contains of a bit more: it also provides Strings (always with double quotes!), it has Numbers (64bit floating point values) Booleans, and it knows null a value for an absent value. And yes, it also has Arrays (a list of arbitrary values) and Objects (a set of unordered key-value pairs where the key is always a string) Commented May 20, 2019 at 8:43

1 Answer 1

8

JavaScript runs on the client computer. Python runs on the server. These are two different machines. They communicate over the network. You can only send sequences of bytes over the network. JSON is a way of serializing objects, i.e. turning Python objects into a portable (i.e. machine, CPU, os independent) sequence of bytes.

There's nothing particularly special about JSON, you could use some other format like XML or even binary protobuf. The good thing about JSON is that both Python and JavaScript have JSON parsers built-in.

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

5 Comments

oh so what it does is convert into 101010001 binary codes and javscript receive this and parse it?
@makewhite Everything in computers is eventually zeroes and ones but it's not really useful to think of content in that way because there would be no difference between XML, JSON, raw memory data and anything else. JSON is text, it's transferred over the network and it has standartised representation and thus parsing rules, hence it makes it easy to share information.
if JSON is a text what is the need for parsing, why not just use it right away?
@makewhite because it's text, it's not code. It's a representation of what an object looks like. Or a data structure, if you will. At any rate, it's not a data structure, similar to how XML will represent some data but you are not expected to just grab the raw XML and use that, you'd do some sort of parsing to turn the XML into the data it describes.
@makewhite Do you know data structures? Text is very difficult data structure to deal with. Arrays, dictionaries, integers, etc. these are data structures that you want to work with. For example, say you get "123" as text and you want to check if it is greater then 0. How would you do that? Remember that a machine sees it as a sequence of 3 bytes. It doesn't know it is a number. So what parsers do is that they give a meaning to a given text converting it into a data structure. JSON is text. In your case array is a more sophisticated data structure (you can loop over it for example).

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.