jsonpath

JsonPath python

眉间皱痕 提交于 2019-12-05 07:07:34
JsonPath JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript, Python, PHP 和 Java。 JsonPath 对于 JSON 来说,相当于 XPATH 对于 XML。 下载地址: https://pypi.python.org/pypi/jsonpath 安装方法:点击 Download URL 链接下载jsonpath,解压之后执行 python setup.py install 官方文档: http://goessner.net/articles/JsonPath JsonPath与XPath语法对比: Json结构清晰,可读性高,复杂度低,非常容易匹配,下表中对应了XPath的用法。 XPath JSONPath 描述 / $ 根节点 . @ 现行节点 / . or [] 取子节点 .. n/a 取父节点,Jsonpath未支持 // .. 就是不管位置,选择所有符合条件的条件 * * 匹配所有元素节点 @ n/a 根据属性访问,Json不支持,因为Json是个Key-value递归结构,不需要。 [] [] 迭代器标示(可以在里边做简单的迭代操作,如数组下标,根据内容选值等) | [,] 支持迭代器中做多选。 [] ?() 支持过滤操作. n/a () 支持表达式计算 () n/a

Find object in nested data by property value (with JSONPath)

情到浓时终转凉″ 提交于 2019-12-05 01:41:44
I have this test data: [ { id: 1, l: 'a', sub: [ ] }, { id: 2, l: 'b', sub: [ { id: 4, l: 'd' }, { id: 5, l: 'e' }, { id: 6, l: 'f', sub: [ { id: 7, l: 'g' } ] } ] }, { id: 3, l: 'c', sub: [] } ]; And I'm trying to get the path of the object with id: 7 . I tried quite some JSONPath queries, but I just can't seem to fiind out how to make JSONPath iterate over all sub keys and search in there. How can I match the object with id: 7 ? Here is my testing plunkr: http://plnkr.co/edit/RoSeRo0L1B2oH3wC5LdU?p=preview This query should work for what you are doing: $..[?(@.id==7)] You need to remove the

Optional JsonPath using Jayway

不羁岁月 提交于 2019-12-05 00:23:54
Question: I have a service that takes in a JSON string as input. The JSON schema is different every time where some fields are not always present. How can I query the values of those fields using Jayway's JsonPath when they are present? What I've tried: I used the Option.DEFAULT_PATH_LEAF_TO_NULL as Jayway's readme page explained Configuration config = Configuration.defaultConfiguration() .addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL); if (JsonPath.isPathDefinite(attribute.jsonPath)) { String value = JsonPath.using(config).parse(currentTx).read(attribute.jsonPath); if (value != null) {

Spring Boot应用的测试——Mockito

痴心易碎 提交于 2019-12-04 23:27:50
Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试;生成测试数据初始化数据库用于测试;Spring Boot可以跟BDD(Behavier Driven Development)工具、Cucumber和Spock协同工作,对应用程序进行测试。 进行软件开发的时候,我们会写很多代码,不过,再过六个月(甚至一年以上)你知道自己的代码怎么运作么?通过测试(单元测试、集成测试、接口测试)可以保证系统的可维护性,当我们修改了某些代码时,通过回归测试可以检查是否引入了新的bug。总得来说,测试让系统不再是一个黑盒子,让开发人员确认系统可用。 在web应用程序中,对Controller层的测试一般有两种方法:(1)发送http请求;(2)模拟http请求对象。第一种方法需要配置回归环境,通过修改代码统计的策略来计算覆盖率;第二种方法是比较正规的思路,但是在我目前经历过的项目中用得不多,今天总结下如何用Mock对象测试Controller层的代码。 在之前的几篇文章中,我们都使用bookpub这个应用程序作为例子,今天也不例外,准备测试它提供的RESTful接口是否能返回正确的响应数据。这种测试不同于 单元测试 ,需要为之初始化完整的应用程序上下文、所有的spring bean都织入以及数据库中需要有测试数据,一般来说这种测试称之为 集成测试 或者

Using Gson with a path

若如初见. 提交于 2019-12-04 11:17:09
Using a simple Json file e.g: {"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }} I want to be able to get the JsonArray named menuitem using a path: String path = "menu.popup.menuitem" I tried to do this using: public static JsonElement fromString(String json, String path) throws JsonSyntaxException { JsonObject obj = GsonBuilder.create().fromJson(json, JsonObject.class); String[] seg = path.split("."); for (String element : seg) { if (obj

Hamcrest with MockMvc: check that key exists but value may be null

假装没事ソ 提交于 2019-12-04 10:40:27
问题 I'm doing some tests with MockMvc, and I want to validate the structure of a JSON response. Specifically, I want to make sure that the key to an attribute exists, and that the value is of a certain type or null. { "keyToNull": null, # This may be null, or a String "keyToString": "some value" } The following works for me, but I'm wondering if there's a way to combine each group of two expectations into a single line, as I have a lot of attributes to check: import static org.springframework

How would you get a JSONPath to all child nodes in an array of JSON object?

纵饮孤独 提交于 2019-12-04 10:35:12
How would you get a JSONPath to all child node of an object? E.g.: var data = [{ "key1": { "children": [{ "key2": "value", "key3": "value", "key4": {} }, { "key2": "value", "key3": "value", "key4": {} }], "key5": "value" } }, { "key1": { "children": { "key2": "value", "key3": "value", "key4": {} }, "key5": "value" } }] I want to get absolute path for all nodes in the data structure as an array: [ "data[0]['key1']['children'][0]['key2']", "data[0]['key1']['children'][0]['key3']", "data[0]['key1']['children'][0]['key4']", ......, "data[0]['key1']['children'][1]['key2']", ......., "data[1]['key1'

How to test if JSON path does not include a specific element, or if the element is present it is null?

梦想与她 提交于 2019-12-04 09:50:00
问题 I have been writing some simple unit testing routines for a simple spring web application. When I add @JsonIgnore annotation on a getter method of a resource, the resulting json object does not include the corresponding json element. So when my unit test routine tries to test if this is null (which is the expected behavior for my case, I don't want the password to be available in json object), test routine runs into an exception: java.lang.AssertionError: No value for JSON path: $.password,

Adding JsonPath in Android Studio causes non-zero exit value 2 error

一个人想着一个人 提交于 2019-12-04 04:35:18
问题 When I add the JsonPath library to my Android Studio (1.4) project I get an error as following: Error:Execution failed for task ':app:dexDebug'. \> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\JDK_64_bit\bin\java.exe'' finished with non-zero exit value 2 (I added the library via File > Project Structure > Dependencies > Library Dependency > com.jayway.jsonpath:json-path:2.0.0 ) Trying to add MultiDex support for

AWS Glue Custom Classifiers Json Path

北城以北 提交于 2019-12-04 03:33:15
问题 I have a set of Json data files that look like this [ {"client":"toys", "filename":"toy1.csv", "file_row_number":1, "secondary_db_index":"4050", "processed_timestamp":1535004075, "processed_datetime":"2018-08-23T06:01:15+0000", "entity_id":"4050", "entity_name":"4050", "is_emailable":false, "is_txtable":false, "is_loadable":false} ] I have created a Glue Crawler with the following custom classifier Json Path $[*] Glue returns the correct schema with the columns correctly identified. However,