Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Get Value From JSON code

By
abdelwahed chiheb
on 11/24/13, 5:07 AM 1,991 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

0
abdelwahed chiheb
On 11/24/13, 11:25 AM

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']

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

Martin
on 11/24/13, 12:04 PM
0

Martin

--Martin--
1978
| 5 6 8
Cornwall, Canada
--Martin--

Analysing, designing, coding and mentoring since 1975. Kilobytes to terabytes. Punch cards to punchy sites. My OpenERP video series: http://www.youtube.com/playlist?list=PLq7op4J183lX44ZlXPiHxUpRvmmRDtxye My Google Speadsheets --> OpenERP Data Pump project: http://martinhbramwell.github.io/GData_OpenERP_Data_Pump My email (with spaces removed) : mhb . warehouseman @ gmail . com

Martin
On 11/24/13, 11:19 AM

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.
0

Martin

--Martin--
1978
| 5 6 8
Cornwall, Canada
--Martin--

Analysing, designing, coding and mentoring since 1975. Kilobytes to terabytes. Punch cards to punchy sites. My OpenERP video series: http://www.youtube.com/playlist?list=PLq7op4J183lX44ZlXPiHxUpRvmmRDtxye My Google Speadsheets --> OpenERP Data Pump project: http://martinhbramwell.github.io/GData_OpenERP_Data_Pump My email (with spaces removed) : mhb . warehouseman @ gmail . com

Martin
On 11/24/13, 11:59 AM

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']

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/24/13, 5:07 AM
Seen: 1991 times
Last updated: 3/16/15, 8:10 AM