[Solved] Parse JSON via Python script

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

[Solved] Parse JSON via Python script

Post by Mr.Dandy »

Dear forum,

I try to parse a JSON string

Code: Select all

{   "name": "United States",   "population": 331002651,   "capital": "Washington D.C.",   "languages": [      "English",      "Spanish"   ]}

Code: Select all

# -*_ coding: utf-8 _*_from __future__ 
import unicode_literals
import uno
import json
def read_value(*args):
    json_content = json.loads(args[0])
    json_key = args[1]
    json_val = json_content[json_key]
    return json_val
    g_exportedScripts = read_value,
This not works with languages key.

Thanks
Attachments
python_json.odt
(13.89 KiB) Downloaded 173 times
Last edited by Mr.Dandy on Wed Mar 09, 2022 10:50 am, edited 1 time in total.
OpenOffice 4.1.12 - Windows 10
JPL
Volunteer
Posts: 130
Joined: Fri Mar 30, 2012 3:14 pm

Re: Parse JSON via Python script

Post by JPL »

For the "Languages" entry, your JSON script returns an array.
The MsgBox statement does not like displaying that array.

JPL
Kubuntu 22.04 / LibO 7.5
Access2Base (LibO).
BaseDocumenter extension (LibO)
ScriptForge (LibO)
Documentation on https://help.libreoffice.org/latest/en- ... bPAR=BASIC
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Parse JSON via Python script

Post by Mr.Dandy »

If I use a variant, I get an error.
OpenOffice 4.1.12 - Windows 10
JPL
Volunteer
Posts: 130
Joined: Fri Mar 30, 2012 3:14 pm

Re: Parse JSON via Python script

Post by JPL »

Code: Select all

MsgBox myarray
is forbidden.

Use f.i.

Code: Select all

MsgBox Join(myarray, ",")
Kubuntu 22.04 / LibO 7.5
Access2Base (LibO).
BaseDocumenter extension (LibO)
ScriptForge (LibO)
Documentation on https://help.libreoffice.org/latest/en- ... bPAR=BASIC
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Parse JSON via Python script

Post by Mr.Dandy »

This is not an issue with Msgbox but that Python script returns.
OpenOffice 4.1.12 - Windows 10
JPL
Volunteer
Posts: 130
Joined: Fri Mar 30, 2012 3:14 pm

Re: Parse JSON via Python script

Post by JPL »

What did you expect as return value other than an array when your JSON string contains

Code: Select all

"languages": [      "English",      "Spanish"   ]
?
Kubuntu 22.04 / LibO 7.5
Access2Base (LibO).
BaseDocumenter extension (LibO)
ScriptForge (LibO)
Documentation on https://help.libreoffice.org/latest/en- ... bPAR=BASIC
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Parse JSON via Python script

Post by Mr.Dandy »

I wait a solution on how to manage this case. :)
OpenOffice 4.1.12 - Windows 10
JPL
Volunteer
Posts: 130
Joined: Fri Mar 30, 2012 3:14 pm

Re: Parse JSON via Python script

Post by JPL »

Replace

Code: Select all

msgbox oPy.invoke(args(), Array(), Array())
with

Code: Select all

ret = oPy.invoke(args(), Array(), Array())
If IsArray(ret) Then msgbox(Join(ret, ",")) Else msgbox(ret)
Kubuntu 22.04 / LibO 7.5
Access2Base (LibO).
BaseDocumenter extension (LibO)
ScriptForge (LibO)
Documentation on https://help.libreoffice.org/latest/en- ... bPAR=BASIC
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Parse JSON via Python script

Post by Mr.Dandy »

Sorry, occurs an error
capture 1.png
OpenOffice 4.1.12 - Windows 10
JPL
Volunteer
Posts: 130
Joined: Fri Mar 30, 2012 3:14 pm

Re: Parse JSON via Python script

Post by JPL »

Works fine in my LibreOffice 7.3 environment.
Sorry. I can't help further.
Kubuntu 22.04 / LibO 7.5
Access2Base (LibO).
BaseDocumenter extension (LibO)
ScriptForge (LibO)
Documentation on https://help.libreoffice.org/latest/en- ... bPAR=BASIC
Bidouille
Volunteer
Posts: 574
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: Parse JSON via Python script

Post by Bidouille »

AOO use the old Python 2.7

json.loads returns an object from a string representing an object.
So use json.dumps to return a string.
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Parse JSON via Python script

Post by Mr.Dandy »

Good catch!

Code: Select all

return json.dumps(json_val)
Thanks
OpenOffice 4.1.12 - Windows 10
Post Reply