This question has been flagged
3 Replies
11031 Views

Hi evryone,

i'm calling a python function from a javascript code, so i'm passing the record with json format. i want to get the value of some fileds from the JSON code. How can i do this ?

image description

Avatar
Discard
Author Best Answer

Hi Martin, thank's .... I tried this code and it work :D

def delete_reserve(self, cr, uid, ids , record,this):
    to_json = json.dumps(record,separators=(','':'))
    data_json = json.loads(to_json)
    wg_product = data_json['attributes']['product_id']
    wg_qty_uos = data_json['attributes']['product_uos_qty']
Avatar
Discard

Glad to help. Pity I missed the karma points by deleting too fast, duh.

Best Answer

Heh!

I deleted my answer because I noticed you wanted JavaScript. You'll see both my earlier versions below:

Improving my old Python answer (below), you may find it far nicer to work with json.loads() with built in conversion to a dot separated namespace object, like this :

data_json = json.loads(
         to_json
       , object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
wg_product = data_json.attributes.product_id
wg_qty_uos = data_json.attributes.product_uos_qty

If you still want JavaScript as you originally asked . . .

  <!DOCTYPE html>
  <html>
  <meta charset="UTF-8">
  <body>

  <p id="rslt">Result here.</p>

  <p id="alt">Alternate result here.</p>

  <button type="button" onclick="tryIt()">Try</button>

  </body>

  <script>

  var jsonGlossary = "{";
  jsonGlossary +=    "  \"glossary\": {";
  jsonGlossary +=    "    \"title\": \"example glossary\",";
  jsonGlossary +=    "    \"GlossDiv\": {";
  jsonGlossary +=    "      \"title\": \"S\",";
  jsonGlossary +=    "      \"GlossList\": {";
  jsonGlossary +=    "        \"GlossEntry\": {";
  jsonGlossary +=    "          \"ID\": \"SGML\",";
  jsonGlossary +=    "          \"SortAs\": \"SGML\",";
  jsonGlossary +=    "          \"GlossTerm\": \"Standard Generalized Markup Language\",";
  jsonGlossary +=    "          \"Acronym\": \"SGML\",";
  jsonGlossary +=    "          \"Abbrev\": \"ISO 8879:1986\",";
  jsonGlossary +=    "          \"GlossDef\": {";
  jsonGlossary +=    "            \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",";
  jsonGlossary +=    "            \"GlossSeeAlso\": [\"GML\", \"XML\"]";
  jsonGlossary +=    "          },";
  jsonGlossary +=    "          \"GlossSee\": \"markup\"";
  jsonGlossary +=    "        }";
  jsonGlossary +=    "      }";
  jsonGlossary +=    "    }";
  jsonGlossary +=    "  }";
  jsonGlossary +=    "}";



  function tryIt()
  {
        obj = JSON.parse(jsonGlossary);
        document.getElementById("rslt").innerHTML=obj['glossary']['GlossDiv']['GlossList']['GlossEntry']['GlossTerm'];
        document.getElementById("alt").innerHTML=obj.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para;
  }
  </script>

  </html>

Not that it matters now, but this was the first answer (that I deleted while you were answering).

  #!/usr/bin/python
  # -*- coding: utf-8 -*-
  #
  from json import loads

  jsonGlossary = '{'
  jsonGlossary += '  "glossary": {'
  jsonGlossary += '    "title": "example glossary",'
  jsonGlossary += '    "GlossDiv": {'
  jsonGlossary += '      "title": "S",'
  jsonGlossary += '      "GlossList": {'
  jsonGlossary += '        "GlossEntry": {'
  jsonGlossary += '          "ID": "SGML",'
  jsonGlossary += '          "SortAs": "SGML",'
  jsonGlossary += '          "GlossTerm": "Standard Generalized Markup Language",'
  jsonGlossary += '          "Acronym": "SGML",'
  jsonGlossary += '          "Abbrev": "ISO 8879:1986",'
  jsonGlossary += '          "GlossDef": {'
  jsonGlossary += '            "para": "A meta-markup language, used to create markup languages such as DocBook.",'
  jsonGlossary += '            "GlossSeeAlso": ["GML", "XML"]'''
  jsonGlossary += '          },'
  jsonGlossary += '          "GlossSee": "markup"'
  jsonGlossary += '        }'
  jsonGlossary += '      }'
  jsonGlossary += '    }'
  jsonGlossary += '  }'
  jsonGlossary += '}'

  print jsonGlossary

  print ' #A - - '
  dictGlossary = loads (jsonGlossary)

  print dictGlossary

  print ' #B - - '
  print dictGlossary['glossary']['GlossDiv']['GlossList']['GlossEntry']['GlossDef']['para']
Avatar
Discard
Best Answer

Hi,

Try something like this :

  #!/usr/bin/python
  # -*- coding: utf-8 -*-
  #
  from json import loads

  jsonGlossary = '{'
  jsonGlossary += '  "glossary": {'
  jsonGlossary += '    "title": "example glossary",'
  jsonGlossary += '    "GlossDiv": {'
  jsonGlossary += '      "title": "S",'
  jsonGlossary += '      "GlossList": {'
  jsonGlossary += '        "GlossEntry": {'
  jsonGlossary += '          "ID": "SGML",'
  jsonGlossary += '          "SortAs": "SGML",'
  jsonGlossary += '          "GlossTerm": "Standard Generalized Markup Language",'
  jsonGlossary += '          "Acronym": "SGML",'
  jsonGlossary += '          "Abbrev": "ISO 8879:1986",'
  jsonGlossary += '          "GlossDef": {'
  jsonGlossary += '            "para": "A meta-markup language, used to create markup languages such as DocBook.",'
  jsonGlossary += '            "GlossSeeAlso": ["GML", "XML"]'''
  jsonGlossary += '          },'
  jsonGlossary += '          "GlossSee": "markup"'
  jsonGlossary += '        }'
  jsonGlossary += '      }'
  jsonGlossary += '    }'
  jsonGlossary += '  }'
  jsonGlossary += '}'

  print jsonGlossary

  print ' #A - - '
  dictGlossary = loads (jsonGlossary)

  print dictGlossary

  print ' #B - - '
  print dictGlossary['glossary']['GlossDiv']['GlossList']['GlossEntry']['GlossDef']['para']

Your output should look this :

{  "glossary": {    "title": "example glossary",    "GlossDiv": {      "title  . . . 
 #A - - 
{u'glossary': {u'GlossDiv': {u'GlossList': {u'GlossEntry': {u'GlossDe . . . 
 #B - - 
A meta-markup language, used to create markup languages such as DocBook.
Avatar
Discard