jsonata-js / jsonata

JSONata query and transformation language - http://jsonata.org
MIT License
2.05k stars 220 forks source link

Multi-level JSON processing: Unwrapping Objects #684

Open bhavaniravi opened 7 months ago

bhavaniravi commented 7 months ago

I have been constructing a JSONata query and it involves multi-level json operation. Transforming and filtering. This is my 1st attempt.

{
    "id": <path>.value,
    "source_id": <path>.value,
    "type": <path>.value,
    "inner_list": <path>.[filter!=null].{
        "index": <path>.value,
        "text": <path>.value,
        "responses": <path>.{
            "index": <path>.value
            "original_response": <path.value>
        }
    }
}[$count(inner_list) != 0]

Things I want to do

  1. Add count(inner_list) as a key/value in the object
  2. After the filtering I want to reconstuct the JSON ie.,
...
...
}[$count(inner_list) != 0]{
    "id": source_id ? id : null,
    "type": type in ["preamble_9", "preamble_10", "preamble_11"] ? "Default Preamble" : type,
   <how-to-preserve-all-the-other-keys-here> 
}

How to preserve/unpack the keys that are untouched?

Note: We will be generating jsonata programatically, so re-writing the whole object again isn't an option