2

How do I access a parent object from a child node. Seems that i can't access the scope

This is the source json

{
    "content" : {
        "date" : "2019-02-10T02:40:48Z",
        "production" : {
            "productionId" : "918",
        }   
    }
}

This is my Jsonata

{
  "productionType": "specificProducts",
  "products": [
    content.production.(
    {"usedProducts" : {
            "id" = productionId,
            "productDate" = content.date  // how do I access content
        }
    })
  ]
}

do I have to save "content" in some kind of variable and pass it to the child ?

Maevy
  • 261
  • 4
  • 24

3 Answers3

2

The Answer is $$.content.date

Here is the documentation of it

https://docs.jsonata.org/programming#built-in-variables

{
  "productionType": "specificProducts",
  "products": [
    content.production.(
    {"usedProducts" : {
            "id" = productionId,
            "productDate" = $$.content.date
        }
    })
  ]
}
Maevy
  • 261
  • 4
  • 24
0

Another solution is to not dive down into the production element until you want to access its 'productionId' property -- like this:

{
    "productionType": "specificProducts",
    "products": [
        content.{
            "usedProducts": {
                 "id": production.productionId,
                 "productDate": date
            }
        }
    ]
}

Then you can just access the 'date' property in the context of its parent content object.

Of course, these answers may or may not work as expected if the source object is more deeply nested, or contains arrays of child objects...

But to answer your original question, "no" -- in JSONata, elements cannot know what "path" was used to dereference them. Iirc, it was a concious design decision to ensure maximum flexibility and speed.

SteveR
  • 1,015
  • 8
  • 12
0

Use the % symbol to access the parent node from the context of a child node. You can use %.% to access the grandparent node, and so on. You can read more about it in the documentation here: https://docs.jsonata.org/path-operators

This might be what you were trying to accomplish. Since the query content.production returns an array, your query had to be adjusted slightly.

{
  "productionType": "specificProducts",
  "products": [
    {
      "usedProducts": [
        content.production.{
            "id": $.productionId,
            "productDate": %.date
        }
      ]
    }
  ]
}
SF1Dev
  • 790
  • 7
  • 11