JSONPath-Plus / JSONPath

A fork of JSONPath from http://goessner.net/articles/JsonPath/
Other
958 stars 169 forks source link

Build a object with JSONPath start from empty object #194

Closed cesco69 closed 1 year ago

cesco69 commented 1 year ago

Is it possible use JSONPath for build an object starting from empty object?

EG:

const data = {}; // start from empty object

const updater = (cb: (value: string) => string): JSONPathCallback => (value, _, { parent, parentProperty }) => {
    parent[parentProperty] = cb(value);
   return parent;
}
// add in book object author name Foo Bar
const r = JSONPath({
    path: '$.book.author',
    json: data,
   callback: updater(() => 'Foo Bar'),
});
console.log(data)

expected output

{
  book: {
    author: 'Foo Bar'
  }
}

output

{}
brettz9 commented 1 year ago

Try something instead like https://www.npmjs.com/package/nested-property#nestedpropertysetdata-path-value .