Your Digital Media Has Never Looked So Good

 
rohitkaushik
Topic Author
Posts: 15
Joined: Wed Sep 10, 2014 1:12 am

how to delete a array from json?

Mon Oct 27, 2014 5:07 am

hi,

I want to delete a array from json.In this example , I want to delete first item whose groupName="Ungrouped".
Please help me.
e.g.--->
{
"destinationContent": [{
"groupName": "Ungrouped",
"isAuth": false,
"groupImgeUrl": "",
"video": []
},
{
"groupName": "grouped",
"isAuth": false,
"groupImgeUrl": "",
"video": []
}]
}
 
dcrandall
Posts: 74
Joined: Wed Jul 30, 2014 11:04 am

Re: how to delete a array from json?

Mon Oct 27, 2014 3:04 pm

A couple ways to skin this cat that might work.

thingToClean = parseJSON(yourJSON)
thingToClean.offendingVariable = invalid
yourJSON = formatJSON(thingToClean)

 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: how to delete a array from json?

Mon Oct 27, 2014 3:48 pm

arrayFromJSON = parseJSON(....)
arrayFromJSON.shift()
' or
arrayFromJSON.delete(0)
 
User avatar
RokuKC
Posts: 315
Joined: Wed Sep 10, 2014 10:44 am
Location: Roku HQ

Re: how to delete a array from json?

Mon Oct 27, 2014 4:45 pm

rohitkaushik wrote:
I want to delete a array from json.In this example , I want to delete first item whose groupName="Ungrouped".
}


Assuming the Ungrouped item is not necessarily the first:

j = ParseJSON(jsonStr)

for i = 0 to j.destinationContent.Count() - 1
    item = j.destinationContent[ i ]
    if item.groupName = "Ungrouped"
        j.destinationContent.Delete(i)
        exit for
    end if
end for

newJsonStr = FormatJSON(j)
 
User avatar
TheEndless
** Valued Community Member **
Posts: 9231
Joined: Mon Oct 04, 2004 10:15 am
Location: US
Contact:

Re: how to delete a array from json?

Mon Oct 27, 2014 8:16 pm

RokuKC wrote:
rohitkaushik wrote:
I want to delete a array from json.In this example , I want to delete first item whose groupName="Ungrouped".
}


Assuming the Ungrouped item is not necessarily the first:

j = ParseJSON(jsonStr)

for i = 0 to j.destinationContent.Count() - 1
    item = j.destinationContent[ i ]
    if item.groupName = "Ungrouped"
        j.destinationContent.Delete(i)
        exit for
    end if
end for

newJsonStr = FormatJSON(j)

That also assumes it's the only one in the array. If there are multiple, you can reverse the loop and do this:
j = ParseJSON(jsonStr)

for i = j.destinationContent.Count() - 1 to 0 step -1
    item = j.destinationContent[ i ]
    if item.groupName = "Ungrouped"
        j.destinationContent.Delete(i)
    end if
end for

newJsonStr = FormatJSON(j)

It's also worth noting that FormatJSON isn't a supported function. If you use it in the wrong place, it could hang or crash the box.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: how to delete a array from json?

Tue Oct 28, 2014 12:10 pm

TheEndless wrote:
RokuKC wrote:
Assuming the Ungrouped item is not necessarily the first:
[code ... /]

That also assumes it's the only one in the array. If there are multiple, you can reverse the loop and do this:
[code ... /]

OP said `I want to delete first item whose groupName="Ungrouped" ` - i think RokuKC gave the broadest possible reading.

It's also worth noting that FormatJSON isn't a supported function. If you use it in the wrong place, it could hang or crash the box.

Hm, true that. Good opportunity to peddle my toJSON() :mrgreen:
 
dcrandall
Posts: 74
Joined: Wed Jul 30, 2014 11:04 am

Re: how to delete a array from json?

Tue Oct 28, 2014 2:58 pm

TheEndless wrote:
It's also worth noting that FormatJSON isn't a supported function. If you use it in the wrong place, it could hang or crash the box.


(queue Kyle's mom from southpark)
What what what?
(/queue)

a) They didn't say anything about it not being supported in the docs. I believe you, but now I'm questioning everything.
http://sdkdocs.roku.com/display/sdkdoc/ ... erasString

b) What situations will hang the box, if I may ask? Inquiring minds want to know.
 
User avatar
TheEndless
** Valued Community Member **
Posts: 9231
Joined: Mon Oct 04, 2004 10:15 am
Location: US
Contact:

Re: how to delete a array from json?

Tue Oct 28, 2014 3:53 pm

dcrandall wrote:
TheEndless wrote:
It's also worth noting that FormatJSON isn't a supported function. If you use it in the wrong place, it could hang or crash the box.


(queue Kyle's mom from southpark)
What what what?
(/queue)

a) They didn't say anything about it not being supported in the docs. I believe you, but now I'm questioning everything.
http://sdkdocs.roku.com/display/sdkdoc/ ... erasString

Well, would you look at that. They documented it, so I guess it is supported now!

dcrandall wrote:
b) What situations will hang the box, if I may ask? Inquiring minds want to know.

In previous tests, we found that an AA with a circular reference would hang the box (for obvious reasons). Now that it's documented, it's possible they put checks in to prevent that.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: how to delete a array from json?

Tue Oct 28, 2014 6:20 pm

TheEndless wrote:
Well, would you look at that. They documented it, so I guess it is supported now!

Apparently. Dog forbid someone let us know about it though, by say publishing release notes or at least reply to that forum thread.
Anybody want to place a bet if formatJSON() as documented works on fw3 ?

TheEndless wrote:
In previous tests, we found that an AA with a circular reference would hang the box (for obvious reasons). Now that it's documented, it's possible they put checks in to prevent that.

Just checked and that was fixed... eh, kind of. It doesn't reboot the player anymore but shows an ugly, UGLY warning which calls itself an ": ERROR:" yet keeps executing (eh?).
 
User avatar
RokuKC
Posts: 315
Joined: Wed Sep 10, 2014 10:44 am
Location: Roku HQ

Re: how to delete a array from json?

Thu Oct 30, 2014 2:10 pm

dcrandall wrote:
TheEndless wrote:
It's also worth noting that FormatJSON isn't a supported function. If you use it in the wrong place, it could hang or crash the box.


ParseJSON and FormatJSON are supported functions for devices running modern firmware.

Bug reports are always welcome. :)
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: how to delete a array from json?

Thu Oct 30, 2014 5:32 pm

RokuKC wrote:
ParseJSON and FormatJSON are supported functions for devices running modern firmware.

parseJSON() is also supported on fw3, which (unusually - but thankfully) was noted in the docs.

No biggie about no formatJSON() on fw3, since that is the easy direction - parsing is the slow one. Thanks for adding version note to it.

Who is online

Users browsing this forum: No registered users and 7 guests