1

Developing in python, I would like to create a json document (tree) from a list of jsonpath and corresponding values.

For example, from the jsonpath

"$.orders[0].client" 

and a value "acme", it would create:

{'orders':[{'client':'acme'}]}

And then, I should be able to add a new node in the json document, at the location specified by another jsonpath with a given value. For example adding jsonpath $.orders[0].orderid and value "123", this would result in an updated json document:

{'orders':[{'client':'acme', 'orderid' : 123}]}

I have tried to understand jsonpath-ng for that purpose, but I don't understand how I could use it for that purpose (and not even if this is possible).

Anyone who could help?

Thanks in advance,

Best regards,

Laurent

blackbishop
  • 30,945
  • 11
  • 55
  • 76

1 Answers1

2

If I understand your question correctly you are asking for a JSON to JSON transformation. JSONPath is not the right tool then. There are specialized libraries for this, e.g. JSONBender. You can transform some JSON like this:

import json
from jsonbender import bend, K, S

MAPPING = {
    'client': S('orders', 0, 'client'),
    'orderid': S('orders', 0, 'orderid')
}

source = {
   "orders" : [
      {
         "orderid": 123,
         "client" : "acme"
      },
      {
         "orderid": 321,
         "client" : "two"
      }
   ]
}

result = bend(MAPPING, source)
print(json.dumps(result))

{"client": "acme", "orderid": 123}

You can certainly bend the result even further but simpler output might work even better.

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Many thanks for your answer. I want to create a json document from scratch, which seems feasible with JSONBlender, using an empty source dict and a mapping with K() selectors. But I have a constraint: the JSON cannot be specified within the code, or as a mapping dict. It should be sourced from a db, editable from a web page in a safe way (no syntax error, easy validation). This is why I tought about JSONPath. Is there a way to convert a JSONPath (+ the value) into a JSONBlender “MAPPING” dict ? Does it make sense? Do you see alternatives? Thanks again! – user9158914 Feb 02 '21 at 07:56
  • @user9158914 I think I have seen something along the lines you have in mind in the Java camp but not for Python; Creating JSON from scratch based on path nodes should be possible but might require multiple steps. JSONPath could be still be used to verify the results. In general, the query and transformation facilities in the JSON world are still lacking compared to XML. – wp78de Feb 02 '21 at 16:02
  • Thanks for your answers. Indeed, they seem a bit more advanced in the Java world, and JSON is lacking tools. I'll write my own simple parser that would convert the JSONPath and associated value into a JSON document. I will then look as the best way to insert this constructed JSON document in a pre-existing target JSON document. This is where JSONBlender could help. Is JSONBlender the best tool for this? If so, how can I use JSONBlender to integrate a JSON document into another ? – user9158914 Feb 04 '21 at 07:36
  • To be honest, I cannot give a good account of the available Python JSON Transformers; searching GitHub yields multiple results (I searched: [python] JSON transformation) but I cannot tell if those are any good or if there is a better place to look. It really looks like the wheat has not been separated from the chaff yet in this area. – wp78de Feb 04 '21 at 15:24
  • Thanks for the hints. I decided to start writing my own code to convert a JSONPath string into a Python dictionary. Then use the jsonmerge library to do the rest. I could start from a JSON formatted string, but this is error prone (syntax), and I also want to separate the selector from the actual value I want to place at the specified location (which is not possible using a JSON string). – user9158914 Feb 13 '21 at 09:25
  • @user9158914 sounds like a practical approach. I am glad I could help. – wp78de Feb 13 '21 at 15:54