python querying a json objectpath

  • Last Update :
  • Techknowledgy :

Is this what you are trying to do?

import objectpath

data = {
   "user": {
      "actions": {
         "name": "reading",
         "description": "blablabla"
      },
      "name": "John"
   }
}

tree = objectpath.Tree(data)
result = tree.execute("$.user[@.name is 'John'].actions[@.name is 'reading'].description")
for entry in result:
   print entry

Output

blablabla
import objectpath
import *

your_json = {
   "name": "felix",
   "last_name": "diaz"
}

# This json path will bring all the key - values of your json

your_json_path = '$.*'

my_key_values = Tree(your_json).execute(your_json_path)

# If you want to retrieve the name node...then specify it.
my_name = Tree(your_json).execute('$.name')

# If you want to retrieve a the last_name node...then specify it.
last_name = Tree(your_json).execute('$.last_name')

I believe you're just missing a comma in JSON:

{
   "user": {
      "actions": [{
         "name": "reading",
         "description": "blablabla"
      }],
      "name": "John"
   }
}

Assuming there is only one "John", with only one "reading" activity, the following query works:

$.user[@.name is 'John'].actions[0][@.name is 'reading'][0].description

If there could be multiple "John"s, with multiple "reading" activities, the following query will almost work:

$.user.*[@.name is 'John'].actions..*[@.name is 'reading'].description

Suggestion : 2

If you feel you could maintain this code, ping me. I’d be more than happy to transfer this repo to a dedicated ObjectPath organization on GitHub and give the ownership to someone with more time for this project than me.,ObjectPath is a query language similar to XPath or JSONPath, but much more powerful thanks to embedded arithmetic calculations, comparison mechanisms and built-in functions. This makes the language more like SQL in terms of expressiveness, but it works over JSON documents rather than relations. ObjectPath can be considered a full-featured expression language. Besides selector mechanism there is also boolean logic, type system and string concatenation available. On top of that, the language implementations (Python at the moment; Javascript is in beta!) are secure and relatively fast.,I appreciate all contributions and bugfix requests for ObjectPath, however since I don’t code in Python any more, this library is not maintained as of now. Since I can’t fully assure that code contributed by others meets quality standards, I can’t accept PRs.,ObjectPath makes it easy to find data in big nested JSON documents. It borrows the best parts from E4X, JSONPath, XPath and SQL. ObjectPath is to JSON documents what XPath is to XML. Other examples to ilustrate this kind of relationship are:

Command line usage

$ sudo pip install objectpath
$ objectpath file.json

or

$ git clone https: //github.com/adriank/ObjectPath.git
   $ cd ObjectPath
$ python shell.py file.json

Python usage

$ sudo pip install objectpath
$ python
   >>>
   from objectpath
import *
>>>
tree = Tree({
      "a": 1
   }) >>>
   tree.execute("$.a")
1
   >>>
$ git clone https: //github.com/adriank/ObjectPath.git
   $ cd ObjectPath
$ python
   >>>
   from objectpath
import *
>>>
tree = Tree({
      "a": 1
   }) >>>
   tree.execute("$.a")
1
   >>>

Suggestion : 3

Last Updated : October 19, 2021

Below given are few examples of JSONPath.

$.store.book[0].title
$.store.book[ * ].title
$..book[3]

//or using brackets

$['store']['book'][0].['title']
$['store']['book'][ * ].['title']
$..['book'][3]

$.store.book[ ? (@.price & lt; 10)].title

We have the following JSON document. We will apply the JSONPath expressions to it.

{
   "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": "fiction",
            "author": "Herman Melville",
            "title": "Moby Dick",
            "isbn": "0-553-21311-3",
            "price": 8.99
         },
         {
            "category": "fiction",
            "author": "J. R. R. Tolkien",
            "title": "The Lord of the Rings",
            "isbn": "0-395-19395-8",
            "price": 22.99
         }
      ],
      "bicycle": {
         "color": "red",
         "price": 19.95
      }
   }
}

Using JSONPath to find the names of all authors.

var response = jsonPath(store, "$..author").toJSONString();

Using JSONPath to find the details for book number 4. The array index is zero-based.

var response = jsonPath(store, "$..book[3]").toJSONString();
public static List readJsonFileDynamic(String filePath, String jsonPath) {

   System.out.println("jsonpath - " + jsonPath);
   try {
      String content = new String(Files.readAllBytes(Paths.get(filePath)));
      Configuration conf = Configuration.builder()
         .jsonProvider(new GsonJsonProvider())
         .mappingProvider(new GsonMappingProvider())
         .build();

      DocumentContext context = JsonPath.using(conf).parse(content);
      categories = context.read(jsonPath, List.class); //List 

   } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
   return categories;
}
1._
[{
      "firstName": "F1",
      "lastName": "L1",
      "address": [{
            "city": "Atlanta",
            "country": "US"
         },
         {
            "city": "Maidson",
            "country": "US"
         }
      ]
   },
   {
      "firstName": "F2",
      "lastName": "L2",
      "address": [{
            "city": "Nashville",
            "country": "US"
         },
         {
            "city": "Vegas",
            "country": "US"
         }
      ]
   },
   {
      "firstName": "F3",
      "lastName": "L3",
      "address": [{
            "city": "Minneapolis",
            "country": "US"
         },
         {
            "city": "Nashville",
            "country": "US"
         }
      ]
   }
]

From the above data, how can I get person details who lived in Nashville for example. I am expecting the following result

[{
      "firstName": "F2",
      "lastName": "L2",
      "address": [{
            "city": "Nashville",
            "country": "US"
         },
         {
            "city": "Vegas",
            "country": "US"
         }
      ]
   },
   {
      "firstName": "F3",
      "lastName": "L3",
      "address": [{
            "city": "Minneapolis",
            "country": "US"
         },
         {
            "city": "Nashville",
            "country": "US"
         }
      ]
   }
]
{
   "kind": "storage#policy",
   "resourceId": "test1",
   "bindings": [{
      "role": "roles/admin",
      "members": ["allUsers"]
   }, ]
}

How can i append two string response from json using jsonpath ?
Ex. JsonResponse

{
   "passport": {
      "page_1": {
         "passport_number": "xxxxxxx",
         "photo": "https://storage.googleapis.com/dolphin-ekyc-api-v2/2018-11-15-xxxx.jpeg",
         "sign": "https://storage.googleapis.com/dolphin-ekyc-api-v2/2018-11-15-xxxx.jpeg",
         "surname": "RAMADUGULA",
         "name": "SITA MAHA LAKSHMI",
         "nationality": "INDIAN",
         "sex": "",
         "date_of_birth": "23/09/xxxx",
         "place_of_birth": "GUNDUGOLANU",
         "date_of_issue": "11/10/xxxx",
         "date_of_expiry": "10/10/xxxx"
      }
   }
}

When i tried using $['passport']['page_1']['name','surname'] its giving me value :

{
   "name": "SITA MAHA LAKSHMI",
   "surname": "RAMADUGULA"
}

Suggestion : 4

I personally went with pyjq because I anycodings_python use jq all the time for data exploration anycodings_python but ObjectPath seems very attractive and anycodings_python not limited to json.,You can also check out PythonQL, a query anycodings_python language extension to Python that anycodings_python handles SQL and JSON queries: pythonql,I notice this question was asked a few anycodings_python years ago but if someone else find this, anycodings_python here are some newer projects trying to anycodings_python address this same problem:,It even appears to have some basic anycodings_python aggregation functions. While not being anycodings_python specific to JSON, I think it's a least a anycodings_python good starting point for querying.

The format the data that I'll be generating anycodings_json is like this:

{
   "Operations": [{
         "OpID": "0",
         "type": "callback",
         "stringTag1": "foo1",
         "stringTag2": "FooMsg",
         "Children": [...],
         "value": "0.000694053"
      },
      {
         "OpID": "1",
         "type": "callback",
         "stringTag1": "moo1",
         "string2": "MooMsg",
         "Children": [...],
         "value": "0.000468427"
      }
   }

It looks as though Python has something anycodings_python similar called Pynq which supports basic anycodings_python querying such as:

filtered_collection = From(some_collection).where("item.property > 10").select_many()