tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.39k stars 857 forks source link

How to pick up specific values from array of objects into target array of objects? #343

Closed manmohantarle closed 11 months ago

manmohantarle commented 11 months ago

Hello @tidwall

I want to get values for some keys from an array of objects and resulting value should also be array of objects with specified keys,

as per given below, source

[ { "name": "tom", "age":37, "children": ["Sara","Alex","Jack"], "fav.movie": "Deer Hunter" }, { "name": "riddle", "age":45, "children": ["Json","James","Jill"], "fav.movie": "Saw" } ]

target json

[ { "name": "tom", "children": ["Sara","Alex","Jack"] }, { "name": "riddle", "children": ["Json","James","Jill"] } ]

how can I achieve this?

volans- commented 11 months ago

You can achieve that with multipaths (see https://github.com/tidwall/gjson/blob/master/SYNTAX.md#multipaths ):

#.{name,children}

On Thu, Dec 21, 2023, 18:34 Manmohan Tarle @.***> wrote:

I want to get values for some keys from an array of objects and resulting value should also be array of objects with specified keys,

as per given below, source

[ { "name": "tom", "age":37, "children": ["Sara","Alex","Jack"], "fav.movie": "Deer Hunter" }, { "name": "riddle", "age":45, "children": ["Json","James","Jill"], "fav.movie": "Saw" } ]

target json

[ { "name": "tom", "children": ["Sara","Alex","Jack"] }, { "name": "riddle", "children": ["Json","James","Jill"] } ]

how can I achieve this?

— Reply to this email directly, view it on GitHub https://github.com/tidwall/gjson/issues/343, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABURVOKFH7OVPNTWMPXDEGLYKRXKVAVCNFSM6AAAAABA6XNXWKVHI2DSMVQWIX3LMV43ASLTON2WKOZSGA2TEOBSGQZDQMY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

manmohantarle commented 11 months ago

that worked, thank you @volans-