jsonpath

Json.NET JSONPath query not returning expected results

假装没事ソ 提交于 2019-12-01 06:54:08
I'm using Newtonsoft's Json.Net to select nodes from the following json: { "projects":[ { "name":"Project 1", "client":{ "code":"ABC", "name":"Client 1" } }, { "name":"Project 2", "client":{ "code":"DEF", "name":"Client 2" } }, { "name":"Project 3", "client":{ "code":"GHI", "name":"Client 3" } } ] } The following c# snippet //json is a JObject representation of the json listed above var clients = json.SelectTokens("$.projects[*].client"); Yields: [ { "code":"ABC", "name":"Client 1" }, { "code":"DEF", "name":"Client 2" }, { "code":"GHI", "name":"Client 3" } ] Which is cool, now, what I'd like

python 处理 json 数据

南楼画角 提交于 2019-12-01 04:59:07
1.通过json包处理json数据 import json #导入json包 json.dumps() # 将字典、列表转化为json格式的字符串 json.loads() #将json 格式的字符串转化为python 对象 json.dump(lt,open('json.txt','w',encoding='utf8')) #将lt 转化为json 格式的字符串写入到文件当中 json.load(open('json.txt','r',encoding='utf8')) #从文件中读取json格式字符串,转化为python对象 2.解析json数据: 解析html 我们常用xpath, 解析json 我们常用jsonpath 1 import jsonpath #导入解析json数据的库 2 import resquests #导入发送请求的库 3 response = resquests.get("XXX") #发送请求得到相应 4 x = jsonpath.jsonpath(eval(response.text), "$..ldtw_f2") #对相应内容进行解析 这里必须用eval()将其转化为pyhton的数据类型(字典或列表),第二个参数是解析表达式类似xpath解析 来源: https://www.cnblogs.com/yanhonghong/p/11656411

Find a JSON property name that starts with something using JSON Path

不想你离开。 提交于 2019-12-01 03:49:00
Is it possible to find a property name that matches a regex pattern (or at least starts with) using JSON Path. In XPath I can use name() but I haven't been able to find the JSON Path equivalent. Basically, I what to find all property names that start with x- . Something like $..x-* . I'll be interested in using any javascript package that does this. Currently, I'm using JSONPath . From my googling this is not possible in standard json path . However, the JSONPath package extends the specification with @path ; and makes the following possible $..[?(@path.includes("[\'x-"))] The above assumes

TypeError: write() argument must be str, not bytes

我与影子孤独终老i 提交于 2019-12-01 01:06:33
ModuleNotFoundError: No module named 'jsonpath' 继上一篇博客关于没有jsonpath解决后出现的新情况 在执行时控制台提示如下错误码: TypeError: write() argument must be str, not bytes 初学者的我看CSDN上很多东抄西抄的也没懂,然后在stackoverflow上找到了想要的答案并解决了我的问题 链接: TypeError: must be str, not bytes 提问是这样的,截个图 回答也是一针见血,截个图 说输出文件应该是二进制模式 修改后控制台执行没问题 然后我在Python文档查询open得知: Python:open/文件操作 f=open('/tmp/hello',' w ') #open(路径+文件名, 读写模式 ) #读写模式:r只读,r+读写, w新建 (会覆盖原有文件),a追加, b二进制文件.常用模式 来源: oschina 链接: https://my.oschina.net/u/2763509/blog/1606344

Find a JSON property name that starts with something using JSON Path

北战南征 提交于 2019-12-01 00:06:54
问题 Is it possible to find a property name that matches a regex pattern (or at least starts with) using JSON Path. In XPath I can use name() but I haven't been able to find the JSON Path equivalent. Basically, I what to find all property names that start with x- . Something like $..x-* . I'll be interested in using any javascript package that does this. Currently, I'm using JSONPath. 回答1: From my googling this is not possible in standard json path . However, the JSONPath package extends the

Testing with spring-test-mvc jsonpath returns null

徘徊边缘 提交于 2019-12-01 00:02:02
I am using Spring's "spring-test-mvc" library to test web controllers. I have a very simple controller that returns a JSON array. Then in my test I have: @Test public void shouldGetAllUsersAsJson() throws Exception { mockMvc.perform(get("/v1/users").accept(MediaType.APPLICATION_JSON)) .andExpect(content().mimeType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("fName").exists()); } The above test returns: java.lang.AssertionError: No value for JSON path: fName To quickly check what I actually get I ran the below test: @Test public void shouldPrintResults() throws Exception { mockMvc.perform

神奇的JsonPath

最后都变了- 提交于 2019-11-30 19:13:20
这篇博文主要参考了JsonPath官方文档https://github.com/json-path/JsonPath,汇总整理的目的是为了未来更方便查阅 JsonPath 类似于XPath在xml文档中的定位,JsonPath表达式通常是用来路径检索或设置Json的。其表达式可以接受“dot–notation”和“bracket–notation”格式,例如$.store.book[0].title、$['store']['book'][0]['title'] 运算符 运算符 说明 $ 根节点,代表整个json对象 @ 表示当前节点,用在过滤判断中 * 通配符,可以表示任意节点名或数字索引 .. 深度扫描,返回同key的value数组 .<name> 点表达式,表示当前节点的子节点 ['<name>' (, '<name>')] 表示一个或多个子节点 [<number> (, <number>)] 通过数组索引来表示 [start:end] 切片运算符 [?(<expression>)] 过滤表达式,表达式结果为布尔值 函数 函数在jsonpath表达式的末尾被调用,并且函数以jsonpath表达式的结果作为输入 函数 说明 输出 min() 获取一个数值数组的最小值 Double max() 获取一个数值数组的最大值 Double avg() 获取一个数值数组的平均值

How do I get a list of all JSON paths to values from a JSON String?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 18:17:30
问题 My goal is to read a JSON file and understand the location of all the values, so that when I encounter that same JSON, I can easily read all the values. I am looking for a way to return a list containing all of the paths to each data value, in Jayway JsonPath format. Example JSON: { "shopper": { "Id": "4973860941232342", "Context": { "CollapseOrderItems": false, "IsTest": false } }, "SelfIdentifiersData": { "SelfIdentifierData": [ { "SelfIdentifierType": { "SelfIdentifierType": "111" } }, {

Editing JSON using JSONPath

亡梦爱人 提交于 2019-11-30 10:15:53
Hello SO community :)! I want to create method which will let user to edit (or add) JSON specific value or object (located in JSON by JSONPath). Below simple example what is on my mind. User always enters JSON, JSONPath and value/object to change. I'm using Json.NET library. Method input {json, jsonpath, valuetoedit} || output {new json as string} Example input: { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category"

JsonPath AND Operator on Array

心不动则不痛 提交于 2019-11-30 08:29:56
问题 I have an array in JSON and I would like to apply two filters to it like so: $._embedded.values[0]._embedded.data[?(@.var1='value1' && @.var2='value2')] I need to select only those elements from the array that satisfies the AND operation. However, in practice this doesn't appear to work. Is it possible to do this or must I perform a two step action to extract one set then filter again to get my final results? 回答1: As far as I know AND / OR are not supported yet. But you can get relatively