Skip to content

Instantly share code, notes, and snippets.

@theory
Last active July 16, 2024 13:41
Show Gist options
  • Save theory/0ba2297258097f16fbacd2c37c92d891 to your computer and use it in GitHub Desktop.
Save theory/0ba2297258097f16fbacd2c37c92d891 to your computer and use it in GitHub Desktop.
JSON Entity Selection

Say you have a JSON entity like this:

{
  "meta": {
    "created_at": "2024-06-25T11:56:42Z",
    "updated_at": "2024-07-12T15:32:24Z",
    "created_by": "theory",
    "updated_by": "strongrrl"
  },
  "contact": {
    "emails": [
      { "type": "work", "email": "[email protected]" },
      { "type": "home", "email": "[email protected]" }
    ],
    "phones": [
      { "type": "work", "phone": "+1-312-555-1212" },
      { "type": "home", "phone": "+1-312-555-2121" }
    ]
  },
  "profile": {
    "name": "Barrack Obama",
    "title": "Former POTUS",
    "org": "The Obama Foundation",
    "ssn": "123-45-6789"
  }
}

And you have an employee who's entitled to look at some but not all of the entity. Perhaps their permissions limit them to metadata dates, email addresses, name, and employer. The resulting entity presented to the employee would be:

{
  "meta": {
    "created_at": "2024-06-25T11:56:42Z",
    "updated_at": "2024-07-12T15:32:24Z"
  },
  "contact": {
    "emails": [
      { "type": "work", "email": "[email protected]" }
    ]
  },
  "profile": {
    "name": "Barrack Obama",
    "org": "The Obama Foundation"
  }
}

Is there some sort of object selector/json selector language or pattern for this? JSONPath is great but only allows a single path, not multiple selects.

{
"meta": {
"created_at": "2024-06-25T11:56:42Z",
"updated_at": "2024-07-12T15:32:24Z",
"created_by": "theory",
"updated_by": "strongrrl"
},
"contact": {
"emails": [
{ "type": "work", "email": "[email protected]" },
{ "type": "home", "email": "[email protected]" }
],
"phones": [
{ "type": "work", "phone": "+1-312-555-1212" },
{ "type": "home", "phone": "+1-312-555-2121" }
]
},
"profile": {
"name": "Barrack Obama",
"title": "Former POTUS",
"org": "The Obama Foundation",
"ssn": "123-45-6789"
}
}
{
"meta": {
"created_at": "2024-06-25T11:56:42Z",
"updated_at": "2024-07-12T15:32:24Z"
},
"contact": {
"emails": [
{ "type": "work", "email": "[email protected]" }
]
},
"profile": {
"name": "Barrack Obama",
"org": "The Obama Foundation"
}
}
@theory
Copy link
Author

theory commented Jul 16, 2024

That…is a very long RFC!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment