Manipulating JSON files from Python

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Manipulating JSON files from Python

Andreas Neumann-4
Hi all,

I would like to write small utility to manipulate metadata in a JSON
file (for QGIS Webclient), like this one:
http://webgis.uster.ch/qgis-web-client-dev/js/GISProjectListing.js

The tool should import the JSON file, change some values and write it back.

The idea would be to update, e.g. the time the data was last updated or
other values.

My questions:

* what library should I use? SimpleJSON? Others?

* Does someone have sample code around that does something similar?

Thanks,
Andreas

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating JSON files from Python

Nathan Woodrow
Python has a builtin JSON module http://docs.python.org/2/library/json.html which reads JSON into a Python dict

- Nathan


On Thu, Nov 15, 2012 at 12:02 AM, Andreas Neumann <[hidden email]> wrote:
Hi all,

I would like to write small utility to manipulate metadata in a JSON
file (for QGIS Webclient), like this one:
http://webgis.uster.ch/qgis-web-client-dev/js/GISProjectListing.js

The tool should import the JSON file, change some values and write it back.

The idea would be to update, e.g. the time the data was last updated or
other values.

My questions:

* what library should I use? SimpleJSON? Others?

* Does someone have sample code around that does something similar?

Thanks,
Andreas

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer


_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating JSON files from Python

Matthias Kuhn
In reply to this post by Andreas Neumann-4
Why not use the default python library? It will probably give the least
dependency problems.

http://docs.python.org/2/library/json.html

On Wed, 2012-11-14 at 14:02 +0100, Andreas Neumann wrote:

> Hi all,
>
> I would like to write small utility to manipulate metadata in a JSON
> file (for QGIS Webclient), like this one:
> http://webgis.uster.ch/qgis-web-client-dev/js/GISProjectListing.js
>
> The tool should import the JSON file, change some values and write it back.
>
> The idea would be to update, e.g. the time the data was last updated or
> other values.
>
> My questions:
>
> * what library should I use? SimpleJSON? Others?
>
> * Does someone have sample code around that does something similar?
>
> Thanks,
> Andreas
>
> _______________________________________________
> Qgis-developer mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/qgis-developer


_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating JSON files from Python

Pēteris Brūns
Matthias and Nathan is a true.
In fact there is not so much choices simplejson now is the same json for
python 2.5 and 2.6 so it's now by default in python.


On 2012.11.14. 15:19, Matthias Kuhn wrote:

> Why not use the default python library? It will probably give the least
> dependency problems.
>
> http://docs.python.org/2/library/json.html
>
> On Wed, 2012-11-14 at 14:02 +0100, Andreas Neumann wrote:
>> Hi all,
>>
>> I would like to write small utility to manipulate metadata in a JSON
>> file (for QGIS Webclient), like this one:
>> http://webgis.uster.ch/qgis-web-client-dev/js/GISProjectListing.js
>>
>> The tool should import the JSON file, change some values and write it back.
>>
>> The idea would be to update, e.g. the time the data was last updated or
>> other values.
>>
>> My questions:
>>
>> * what library should I use? SimpleJSON? Others?
>>
>> * Does someone have sample code around that does something similar?
>>
>> Thanks,
>> Andreas
>>
>> _______________________________________________
>> Qgis-developer mailing list
>> [hidden email]
>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
> _______________________________________________
> Qgis-developer mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/qgis-developer


--
Pēteris Brūns

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating JSON files from Python

Andreas Neumann-4
In reply to this post by Matthias Kuhn
Thanks all for your hints.

Will have a look at the built-in json module.

Will have to find out how the dictionary looks like, that python is
generating and how to traverse it.

Thanks,
Andreas

Am 14.11.2012 14:19, schrieb Matthias Kuhn:

> Why not use the default python library? It will probably give the least
> dependency problems.
>
> http://docs.python.org/2/library/json.html
>
> On Wed, 2012-11-14 at 14:02 +0100, Andreas Neumann wrote:
>> Hi all,
>>
>> I would like to write small utility to manipulate metadata in a JSON
>> file (for QGIS Webclient), like this one:
>> http://webgis.uster.ch/qgis-web-client-dev/js/GISProjectListing.js
>>
>> The tool should import the JSON file, change some values and write it back.
>>
>> The idea would be to update, e.g. the time the data was last updated or
>> other values.
>>
>> My questions:
>>
>> * what library should I use? SimpleJSON? Others?
>>
>> * Does someone have sample code around that does something similar?
>>
>> Thanks,
>> Andreas
>>
>> _______________________________________________
>> Qgis-developer mailing list
>> [hidden email]
>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>
> _______________________________________________
> Qgis-developer mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating JSON files from Python

gene
see also json – JavaScript Object Notation Serializer in the series "Python Module of the Week" of Doug Helmann. Sean Gillies (Shapely) also developed GeoJSON (http://pypi.python.org/pypi/geojson/1.0.1) for geospatial purposes.
For others implementations see http://pypi.python.org/pypi?%3Aaction=search&term=json&submit=search

As already mentioned, json, once loaded into Python is nothing more than a dictionary.
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating JSON files from Python

Noli Sicad
This might help.

Tablib: Pythonic Tabular Datasets

http://docs.python-tablib.org/en/latest/


tablib.Dataset()
    A Dataset is a table of tabular data. It may or may not have a
header row. They can be build and manipulated as raw Python datatypes
(Lists of tuples|dictionaries). Datasets can be imported from JSON,
YAML, and CSV; they can be exported to XLSX, XLS, ODS, JSON, YAML,
CSV, TSV, and HTML.

tablib.Databook()
    A Databook is a set of Datasets. The most common form of a
Databook is an Excel file with multiple spreadsheets. Databooks can be
imported from JSON and YAML; they can be exported to XLSX, XLS, ODS,
JSON, and YAML.

https://github.com/kennethreitz/tablib

Noli

On 11/15/12, gene <[hidden email]> wrote:

> see also  json – JavaScript Object Notation Serializer
> <http://www.doughellmann.com/PyMOTW/json/>   in the series "Python Module of
> the Week" of Doug Helmann. Sean Gillies (Shapely) also developed GeoJSON (
> http://pypi.python.org/pypi/geojson/1.0.1
> <http://pypi.python.org/pypi/geojson/1.0.1>  ) for geospatial purposes.
> For others implementations see
> http://pypi.python.org/pypi?%3Aaction=search&term=json&submit=search
> <http://pypi.python.org/pypi?%3Aaction=search&term=json&submit=search>
>
> As already mentioned, json, once loaded into Python is nothing more than a
> dictionary.
>
>
>
> --
> View this message in context:
> http://osgeo-org.1560.n6.nabble.com/Manipulating-JSON-files-from-Python-tp5016253p5016491.html
> Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
> _______________________________________________
> Qgis-developer mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating JSON files from Python

Pēteris Brūns
Probably in other case yes, but for Andreas required things better is to
use python built in functionality and escape from external dependencies.

Regards,

On 2012.11.15. 09:34, Noli Sicad wrote:

> This might help.
>
> Tablib: Pythonic Tabular Datasets
>
> http://docs.python-tablib.org/en/latest/
>
>
> tablib.Dataset()
>      A Dataset is a table of tabular data. It may or may not have a
> header row. They can be build and manipulated as raw Python datatypes
> (Lists of tuples|dictionaries). Datasets can be imported from JSON,
> YAML, and CSV; they can be exported to XLSX, XLS, ODS, JSON, YAML,
> CSV, TSV, and HTML.
>
> tablib.Databook()
>      A Databook is a set of Datasets. The most common form of a
> Databook is an Excel file with multiple spreadsheets. Databooks can be
> imported from JSON and YAML; they can be exported to XLSX, XLS, ODS,
> JSON, and YAML.
>
> https://github.com/kennethreitz/tablib
>
> Noli
>
> On 11/15/12, gene <[hidden email]> wrote:
>> see also  json – JavaScript Object Notation Serializer
>> <http://www.doughellmann.com/PyMOTW/json/>   in the series "Python Module of
>> the Week" of Doug Helmann. Sean Gillies (Shapely) also developed GeoJSON (
>> http://pypi.python.org/pypi/geojson/1.0.1
>> <http://pypi.python.org/pypi/geojson/1.0.1>  ) for geospatial purposes.
>> For others implementations see
>> http://pypi.python.org/pypi?%3Aaction=search&term=json&submit=search
>> <http://pypi.python.org/pypi?%3Aaction=search&term=json&submit=search>
>>
>> As already mentioned, json, once loaded into Python is nothing more than a
>> dictionary.
>>
>>
>>
>> --
>> View this message in context:
>> http://osgeo-org.1560.n6.nabble.com/Manipulating-JSON-files-from-Python-tp5016253p5016491.html
>> Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
>> _______________________________________________
>> Qgis-developer mailing list
>> [hidden email]
>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>
> _______________________________________________
> Qgis-developer mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/qgis-developer


--
Pēteris Brūns
SunGIS
+371 26336691
[hidden email]

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating JSON files from Python

Noli Sicad
Editing while reading in tablib -  pull request

https://github.com/kennethreitz/tablib/pull/84

Noli



On 11/15/12, Pēteris Brūns <[hidden email]> wrote:

> Probably in other case yes, but for Andreas required things better is to
> use python built in functionality and escape from external dependencies.
>
> Regards,
>
> On 2012.11.15. 09:34, Noli Sicad wrote:
>> This might help.
>>
>> Tablib: Pythonic Tabular Datasets
>>
>> http://docs.python-tablib.org/en/latest/
>>
>>
>> tablib.Dataset()
>>      A Dataset is a table of tabular data. It may or may not have a
>> header row. They can be build and manipulated as raw Python datatypes
>> (Lists of tuples|dictionaries). Datasets can be imported from JSON,
>> YAML, and CSV; they can be exported to XLSX, XLS, ODS, JSON, YAML,
>> CSV, TSV, and HTML.
>>
>> tablib.Databook()
>>      A Databook is a set of Datasets. The most common form of a
>> Databook is an Excel file with multiple spreadsheets. Databooks can be
>> imported from JSON and YAML; they can be exported to XLSX, XLS, ODS,
>> JSON, and YAML.
>>
>> https://github.com/kennethreitz/tablib
>>
>> Noli
>>
>> On 11/15/12, gene <[hidden email]> wrote:
>>> see also  json – JavaScript Object Notation Serializer
>>> <http://www.doughellmann.com/PyMOTW/json/>   in the series "Python Module
>>> of
>>> the Week" of Doug Helmann. Sean Gillies (Shapely) also developed GeoJSON
>>> (
>>> http://pypi.python.org/pypi/geojson/1.0.1
>>> <http://pypi.python.org/pypi/geojson/1.0.1>  ) for geospatial purposes.
>>> For others implementations see
>>> http://pypi.python.org/pypi?%3Aaction=search&term=json&submit=search
>>> <http://pypi.python.org/pypi?%3Aaction=search&term=json&submit=search>
>>>
>>> As already mentioned, json, once loaded into Python is nothing more than
>>> a
>>> dictionary.
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://osgeo-org.1560.n6.nabble.com/Manipulating-JSON-files-from-Python-tp5016253p5016491.html
>>> Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
>>> _______________________________________________
>>> Qgis-developer mailing list
>>> [hidden email]
>>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>>
>> _______________________________________________
>> Qgis-developer mailing list
>> [hidden email]
>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>
> --
> Pēteris Brūns
> SunGIS
> +371 26336691
> [hidden email]
>
> _______________________________________________
> Qgis-developer mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating JSON files from Python

Andreas Neumann-4
Hi Noli,

Thanks for your hint, but as Peteris said, I am not dealing with flat
data structure (tabular data), but with nested data structure (more like
typical xml files). I think I will try with the builtin json module as
most people suggested.

Andreas

Am 15.11.2012 08:49, schrieb Noli Sicad:
> Editing while reading in tablib -  pull request
>
> https://github.com/kennethreitz/tablib/pull/84
>
> Noli
>
>
>

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer