Dict Vs Json

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

const json = '{"result":true, "count":42}';

const obj = JSON.parse(json); //json string --> json object


const t1 = JSON.stringify(obj) //json object ----> json striing
console.log(obj)
console.log(t1)

//in python dictionary we have any value as our key but key must be immutable . we
can have variable key types also in python dict like :

//Dict = { 1: 'Geeks', int -->string


// 2: 'For', int --->string
// 3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}, int----->dict
// 'name':'Abhishek', string --->string
// }

//but javascript object we basically declare them as attribute

--------------------1(js)
//JavaScript object key names must adhere to some //restrictions to be valid. Key
names must either be //strings or valid identifier or variable names
//in javascript if key constains a space/symbol it must be writtin in quotes

//---------------------2(pyhton)

//in python we cant declare any mutable element as key like list map etc .
//tuple can be used
Keys in Python dictionaries must be hashable (e.g. a string, a number, a float),
while JavaScript does not have such a requirement.

The following is a valid object in JavaScript:

const javascriptObject = { name: 'Alexander Pushkin', year: 1799 }


However, it would be invalid as a Python dictionary:

python_dictionary = {name: 'Alexander Pushkin', year: 1799}

# Results in a NameError: name 'name' is not defined


A quick fix would be to convert the Python dictionary's keys into strings:

my_dictionary = {'name': 'Alexander Pushkin', 'year': 1799}

You might also like