jshon icon indicating copy to clipboard operation
jshon copied to clipboard

Having trouble creating simple json

Open hacktek opened this issue 10 years ago • 2 comments

Hey, great job on this.

I'm trying to create what I believe would be a pretty simple json with nested objects very similar to Youtube's "add to playlist":

{ 'snippet': { 'playlistId': '{PLAYLIST_ID}', 'resourceId': { 'kind': 'youtube#video', 'videoId': '{VIDEO_ID}' } 'position': 0 } }

However, I for the life of me can't get "playlistId" to be inside of "snippet". Is this possible with jshon? What am I missing?

Thanks!

hacktek avatar Dec 14 '14 18:12 hacktek

Answering my own question here, seems I was able to do it like this:

$ jshon -n {} -i snippet -a -n {} -p -s 12345 -i playlistId -p -a -n {} -i resourceId -e resourceId -s youtube -i kind -s 67890 -i videoId -p -s 0 -i position -p <<< "{}" { "snippet": { "playlistId": "12345", "resourceId": { "kind": "youtube", "videoId": "67890" }, "position": "0" } }

Not sure if it's efficiend but it's working.

hacktek avatar Dec 14 '14 18:12 hacktek

Instead of building up the entire JSON object by hand, you may want to use jshon to edit a template structure. This should simplify your code. Here's an example. This takes this structure as input:

{ "snippet": { "position": 0, "playlistId": 33, "resourceId": { "kind": "youtube", "videoId": 67890 } } }

edits it like this:

jshon <<< '{"snippet":{"playlistId":33,"resourceId":{"kind":"youtube","videoId":67890},"position":0}}' -e snippet -s 34 -i playlistId -e resourceId -s 67891 -i videoId -p -p

and returns this structure:

{ "snippet": { "position": 0, "playlistId": "34", "resourceId": { "kind": "youtube", "videoId": "67891" } } }

-- Bill

On Sun, Dec 14, 2014 at 8:53 PM, hacktek [email protected] wrote:

Answering my own question here, seems I was able to do it like this:

$ jshon -n {} -i snippet -a -n {} -p -s 12345 -i playlistId -p -a -n {} -i resourceId -e resourceId -s youtube -i kind -s 67890 -i videoId -p -s 0 -i position -p <<< "{}" { "snippet": { "playlistId": "12345", "resourceId": { "kind": "youtube", "videoId": "67890" }, "position": "0" } }

Not sure if it's efficiend but it's working.

— Reply to this email directly or view it on GitHub https://github.com/keenerd/jshon/issues/37#issuecomment-66924496.

zot avatar Dec 15 '14 07:12 zot