使用路径表达式字符串获取和设置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 GridBagLayout不填充区域   java Memozied Fibonacci未运行与常规Fibonacci解决方案   Java Web启动未启动问题   Java中异常和if-then的区别   java从命令提示符运行批处理文件获取错误   socket在Java中验证SSL证书的公共名称   如何在JAVA中检查字符串数组中的相等字   用java语言将音频文件转换成文本文件的语音识别   java为什么foo(1,2,3)没有传递给varargs方法foo(对象…)作为整数[]   java通过蓝牙将奇怪的数据从Arduino传输到Android   java ContainerRequestFilter获取空entitystream   java如何从安卓 studio中删除不兼容类型错误   基本Java错误   在Spring引导中使用REST API时发生java错误   javascript通过从SQL查询派生的URL打开页面