使用路径表达式字符串获取和设置json中的值

jsonxs的Python项目详细描述


jsonxs是一个python库,它使用路径表达式字符串来获取和 在json和python数据结构中设置值。有点像 jsonpath,但只支持更简单的表达式并允许 修改。

jsonxs可以安全地与不受信任的输入一起使用。

示例

给定以下数据结构:

d = {
  'feed': {
    'id': 'my_feed',
    'url': 'http://example.com/feed.rss',
    'tags': ['devel', 'example', 'python'],
    'short.desc': 'A feed',
  }
}

我们可以从结构中获取、设置和删除值。

jsonxs(d, 'feed.id')                                  # Get the 'id' field: 'my_feed'
jsonxs(d, 'feed.tags')                                # Get the list of tags.
jsonxs(d, 'feed.tags[-1]')                            # Get the last item in the 'tags' list: 'python'
jsonxs(d, 'feed.short\.desc')                         # Escape special char
jsonxs(d, 'feed.long\.desc', default='N/A')           # Return default when key not found
jsonxs(d, 'feed.id', ACTION_SET, 'your_feed')         # Set feed id to 'your_feed'
jsonxs(d, 'feed.tags[-1]', ACTION_SET, 'javascript')  # Replace 'python' with 'javascript'
jsonxs(d, 'feed.tags[0]', ACTION_DEL)                 # Delete the first tag
jsonxs(d, 'feed.url', ACTION_DEL)                     # Delete the 'url' key from a dict
jsonxs(d, 'feed.tags', ACTION_APPEND, 'programming')  # Append 'programming' to tags
jsonxs(d, 'feed.tags[1]', ACTION_INSERT, 'tech')      # Insert 'tech' at pos 1 in tags
jsonxs(d, 'feed.author', ACTION_SET, 'Ferry Boender') # Create new key/string value in dict
jsonxs(d, 'feed.details', ACTION_MKDICT)              # Create new key/dict value in dict
jsonxs(d, 'feed.details', ACTION_MKLIST)              # Create new key/list value in dict

安装和使用

要安装:

pip install jsonxs

使用:

import jsonxs

data = {'foo': 1}
jsonxs.jsonxs(data, 'bar', jsonxs.ACTION_SET, 2)
print data  # {'foo': 1, 'bar': 2}

许可证

jsonxs根据mit许可证获得许可:

版权所有(c)2015 Ferry Boender(ferry.boender@gmail.com

特此免费向任何获得 本软件和相关文档文件的副本 “软件”),无限制地处理软件,包括 但不限于使用、复制、修改、合并、发布, 分发、再授权和/或出售软件副本,以及 允许向其提供软件的人员这样做,但须遵守 以下条件:

上述版权公告及本许可公告须包括在内 在软件的所有副本或大部分中。

软件按“原样”提供,不作任何形式的保证 或默示的,包括但不限于 适销性、适合特定目的和不侵权。 在任何情况下,作者或版权持有人均不对任何 索赔、损害赔偿或其他责任,无论是在合同诉讼中, 侵权行为或其他,由 软件或软件中的使用或其他交易。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java OnResizeListener或OnDrawListener或类似的东西   java Orika映射嵌套子列表   保存时java Heroku请求超时代码H12   数据库在Java中出现socket读取超时异常的原因是什么?   java如何更改来自Sqlite数据库的特定数据在Listview中的行颜色   java JAXB解组器无法正确处理XML中的列表   java Android日期时区让我抓狂   java不透明属性在Swing中如何工作?   eclipse从JavaEE代码生成流程图   java如何在Hibernate中从相关表中获取计数   java Glassfish部署了项目的依赖项库   java使内容适合JavaFx中的WebView   java不满意的链接错误libcrypto。所以1.0.0   循环中java数组的使用   java找出哪个包调用服务