json parse problem

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

json parse problem

Roelof
Hello,

I try to parse a json that differs some times

after parsing with neojson I see something like this 

"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s0' 'x'->0 'y'->0 )) 'width'->404 ))"


is there a way I can filter out a dictonary where the name is equal to z4

Roelof

Reply | Threaded
Open this post in threaded view
|

Re: json parse problem

Ben Coman


On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to parse a json that differs some times

after parsing with neojson I see something like this 

"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s0' 'x'->0 'y'->0 )) 'width'->404 ))"


is there a way I can filter out a dictonary where the name is equal to z4

Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude
but when filtering any collection type, #select: #detect and #reject: are you main go to messages.
If you inspect your dictionary and on the meta tab filter for ...*select*... 
you'll find #associationsSelect:
which works like this...

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Dictionary [1 item] (1->2 )

I notice there is not a similar #associationsReject: 
so if needed you could add your own considering...
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [1 item] (1->2)

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [2 items] (5->6 3->4)

cheers -ben
Reply | Threaded
Open this post in threaded view
|

Re: json parse problem

Roelof
Thanks,

With your hint I solved it this way :

imageUrl1 := levelsJson select: [ :each | (each at: #name) = 'z4' ].
imageUrl := (((imageUrl1 at: 1) at: #tiles) at: 1) at: #url

Roelof



Op 16-3-2019 om 01:04 schreef Ben Coman:


On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to parse a json that differs some times

after parsing with neojson I see something like this 

"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s0' 'x'->0 'y'->0 )) 'width'->404 ))"


is there a way I can filter out a dictonary where the name is equal to z4

Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude
but when filtering any collection type, #select: #detect and #reject: are you main go to messages.
If you inspect your dictionary and on the meta tab filter for ...*select*... 
you'll find #associationsSelect:
which works like this...

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Dictionary [1 item] (1->2 )

I notice there is not a similar #associationsReject: 
so if needed you could add your own considering...
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [1 item] (1->2)

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [2 items] (5->6 3->4)

cheers -ben

Reply | Threaded
Open this post in threaded view
|

Re: json parse problem

Ben Coman


On Sat, 16 Mar 2019 at 17:05, Roelof Wobben <[hidden email]> wrote:
Thanks,

With your hint I solved it this way :

imageUrl1 := levelsJson select: [ :item | (item at: #name) = 'z4' ].
imageUrl := (((imageUrl1 at: 1) at: #tiles) at: 1) at: #url

Cool. Minor tweak... if you use #detect: instead of #select:   
which returns the first matched item rather than a collection
so you wont need ....(imageUrl1 at: 1).... part.

cheers -ben 
 

Roelof



Op 16-3-2019 om 01:04 schreef Ben Coman:


On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to parse a json that differs some times

after parsing with neojson I see something like this 

"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s0' 'x'->0 'y'->0 )) 'width'->404 ))"


is there a way I can filter out a dictonary where the name is equal to z4

Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude
but when filtering any collection type, #select: #detect and #reject: are you main go to messages.
If you inspect your dictionary and on the meta tab filter for ...*select*... 
you'll find #associationsSelect:
which works like this...

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Dictionary [1 item] (1->2 )

I notice there is not a similar #associationsReject: 
so if needed you could add your own considering...
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [1 item] (1->2)

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [2 items] (5->6 3->4)

cheers -ben

Reply | Threaded
Open this post in threaded view
|

Re: json parse problem

Roelof
oke,

how do I then get the url of the line where the name is z4.

I need that because I need that line and then the tiles part which is also a collection and then the url part which also is a part of a collection

Roelof



Op 16-3-2019 om 16:24 schreef Ben Coman:


On Sat, 16 Mar 2019 at 17:05, Roelof Wobben <[hidden email]> wrote:
Thanks,

With your hint I solved it this way :

imageUrl1 := levelsJson select: [ :item | (item at: #name) = 'z4' ].
imageUrl := (((imageUrl1 at: 1) at: #tiles) at: 1) at: #url

Cool. Minor tweak... if you use #detect: instead of #select:   
which returns the first matched item rather than a collection
so you wont need ....(imageUrl1 at: 1).... part.

cheers -ben 
 

Roelof



Op 16-3-2019 om 01:04 schreef Ben Coman:


On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to parse a json that differs some times

after parsing with neojson I see something like this 

"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s0' 'x'->0 'y'->0 )) 'width'->404 ))"


is there a way I can filter out a dictonary where the name is equal to z4

Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude
but when filtering any collection type, #select: #detect and #reject: are you main go to messages.
If you inspect your dictionary and on the meta tab filter for ...*select*... 
you'll find #associationsSelect:
which works like this...

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Dictionary [1 item] (1->2 )

I notice there is not a similar #associationsReject: 
so if needed you could add your own considering...
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [1 item] (1->2)

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [2 items] (5->6 3->4)

cheers -ben


Reply | Threaded
Open this post in threaded view
|

Re: json parse problem

Ben Coman


On Sat, 16 Mar 2019 at 23:29, Roelof Wobben <[hidden email]> wrote:
oke,

how do I then get the url of the line where the name is z4.

I haven't directly tested, but probably...
imageUrl1 := levelsJson detect: [ :item | (item at: #name) = 'z4' ].
imageUrl := ((imageUrl1 at: #tiles) at: 1) at: #url
 
cheers -ben

I need that because I need that line and then the tiles part which is also a collection and then the url part which also is a part of a collection

Roelof



Op 16-3-2019 om 16:24 schreef Ben Coman:


On Sat, 16 Mar 2019 at 17:05, Roelof Wobben <[hidden email]> wrote:
Thanks,

With your hint I solved it this way :

imageUrl1 := levelsJson select: [ :item | (item at: #name) = 'z4' ].
imageUrl := (((imageUrl1 at: 1) at: #tiles) at: 1) at: #url

Cool. Minor tweak... if you use #detect: instead of #select:   
which returns the first matched item rather than a collection
so you wont need ....(imageUrl1 at: 1).... part.

cheers -ben 
 

Roelof



Op 16-3-2019 om 01:04 schreef Ben Coman:


On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to parse a json that differs some times

after parsing with neojson I see something like this 

"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s0' 'x'->0 'y'->0 )) 'width'->404 ))"


is there a way I can filter out a dictonary where the name is equal to z4

Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude
but when filtering any collection type, #select: #detect and #reject: are you main go to messages.
If you inspect your dictionary and on the meta tab filter for ...*select*... 
you'll find #associationsSelect:
which works like this...

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Dictionary [1 item] (1->2 )

I notice there is not a similar #associationsReject: 
so if needed you could add your own considering...
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [1 item] (1->2)

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [2 items] (5->6 3->4)

cheers -ben


Reply | Threaded
Open this post in threaded view
|

Re: json parse problem

Roelof
oke,

then I misunderstood you.
I thought it could be done in one step.

but detect instead of select works.

Roelof



Op 16-3-2019 om 16:32 schreef Ben Coman:


On Sat, 16 Mar 2019 at 23:29, Roelof Wobben <[hidden email]> wrote:
oke,

how do I then get the url of the line where the name is z4.

I haven't directly tested, but probably...
imageUrl1 := levelsJson detect: [ :item | (item at: #name) = 'z4' ].
imageUrl := ((imageUrl1 at: #tiles) at: 1) at: #url
 
cheers -ben

I need that because I need that line and then the tiles part which is also a collection and then the url part which also is a part of a collection

Roelof



Op 16-3-2019 om 16:24 schreef Ben Coman:


On Sat, 16 Mar 2019 at 17:05, Roelof Wobben <[hidden email]> wrote:
Thanks,

With your hint I solved it this way :

imageUrl1 := levelsJson select: [ :item | (item at: #name) = 'z4' ].
imageUrl := (((imageUrl1 at: 1) at: #tiles) at: 1) at: #url

Cool. Minor tweak... if you use #detect: instead of #select:   
which returns the first matched item rather than a collection
so you wont need ....(imageUrl1 at: 1).... part.

cheers -ben 
 

Roelof



Op 16-3-2019 om 01:04 schreef Ben Coman:


On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to parse a json that differs some times

after parsing with neojson I see something like this 

"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s0' 'x'->0 'y'->0 )) 'width'->404 ))"


is there a way I can filter out a dictonary where the name is equal to z4

Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude
but when filtering any collection type, #select: #detect and #reject: are you main go to messages.
If you inspect your dictionary and on the meta tab filter for ...*select*... 
you'll find #associationsSelect:
which works like this...

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Dictionary [1 item] (1->2 )

I notice there is not a similar #associationsReject: 
so if needed you could add your own considering...
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [1 item] (1->2)

d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ]
d inspect. ==> Array [2 items] (5->6 3->4)

cheers -ben



Reply | Threaded
Open this post in threaded view
|

Re: json parse problem

Sven Van Caekenberghe-2
In reply to this post by Ben Coman


> On 16 Mar 2019, at 16:32, Ben Coman <[hidden email]> wrote:
>
>
>
> On Sat, 16 Mar 2019 at 23:29, Roelof Wobben <[hidden email]> wrote:
> oke,
>
> how do I then get the url of the line where the name is z4.
>
> I haven't directly tested, but probably...
> imageUrl1 := levelsJson detect: [ :item | (item at: #name) = 'z4' ].
> imageUrl := ((imageUrl1 at: #tiles) at: 1) at: #url

If you are not using mapping, you get Dictionary and Array instances back. You can substitute Dictionary for NeoJSONObject, which is a Dictionary subclass that behaves more like a JavaScript object (which is good or bad, depending on your point of view).

  obj := NeoJSONObject fromString: '{"foo":1,"bar":-2}'.

Given the above, you can do

  obj foo
  obj bar

It is as if each key has an accessor (and mutator as well). If there is no such key, nil is returned, duh.

So you could write Ben's code as

  imageUrl1 := levelsJson detect: [ :item | item name = 'z4' ].
  imageUrl := imageUrl1 tiles first url.

Which you might like more.

> cheers -ben
>
> I need that because I need that line and then the tiles part which is also a collection and then the url part which also is a part of a collection
>
> Roelof
>
>
>
> Op 16-3-2019 om 16:24 schreef Ben Coman:
>>
>>
>> On Sat, 16 Mar 2019 at 17:05, Roelof Wobben <[hidden email]> wrote:
>> Thanks,
>>
>> With your hint I solved it this way :
>>
>> imageUrl1 := levelsJson select: [ :item | (item at: #name) = 'z4' ].
>> imageUrl := (((imageUrl1 at: 1) at: #tiles) at: 1) at: #url
>>
>> Cool. Minor tweak... if you use #detect: instead of #select:  
>> which returns the first matched item rather than a collection
>> so you wont need ....(imageUrl1 at: 1).... part.
>>
>> cheers -ben
>>  
>>
>> Roelof
>>
>>
>>
>> Op 16-3-2019 om 01:04 schreef Ben Coman:
>>>
>>>
>>> On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <[hidden email]> wrote:
>>> Hello,
>>>
>>> I try to parse a json that differs some times
>>>
>>> after parsing with neojson I see something like this  
>>>
>>> "an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s0
>>> ' 'x'->0 'y'->0 )) 'width'->404 ))"
>>>
>>>
>>> is there a way I can filter out a dictonary where the name is equal to z4
>>>
>>>
>>> Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude
>>> but when filtering any collection type, #select: #detect and #reject: are you main go to messages.
>>> If you inspect your dictionary and on the meta tab filter for ...*select*...
>>> you'll find #associationsSelect:
>>> which works like this...
>>>
>>> d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
>>> d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ]
>>> d inspect. ==> Dictionary [1 item] (1->2 )
>>>
>>> I notice there is not a similar #associationsReject:
>>> so if needed you could add your own considering...
>>> d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
>>> d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ]
>>> d inspect. ==> Array [1 item] (1->2)
>>>
>>> d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ).
>>> d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ]
>>> d inspect. ==> Array [2 items] (5->6 3->4)
>>>
>>> cheers -ben
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: json parse problem

Roelof
Op 16-3-2019 om 18:16 schreef Sven Van Caekenberghe:
  imageUrl1 := levelsJson detect: [ :item | item name = 'z4' ].
  imageUrl := imageUrl1 tiles first url.


nope,

when I do this I see this error messsage :

MessageNotUnderstood: Dictionary>>name

Roelof


Reply | Threaded
Open this post in threaded view
|

Re: json parse problem

Sven Van Caekenberghe-2


> On 16 Mar 2019, at 21:17, Roelof Wobben <[hidden email]> wrote:
>
> Op 16-3-2019 om 18:16 schreef Sven Van Caekenberghe:
>>   imageUrl1 := levelsJson detect: [ :item | item name = 'z4' ].
>>   imageUrl := imageUrl1 tiles first url.
>>
>
>
> nope,
>
> when I do this I see this error messsage :
>
> MessageNotUnderstood: Dictionary>>name
>
> Roelof

Yes, but you have to parse your JSON differently, either doing

 NeoJSONObject fromString: ..

or, alternatively you have to set the #mapClass, like this

 (NeoJSONReader on: string readStream)
    mapClass: NeoJSONObject;
    propertyNamesAsSymbols: true;
    next.

I don't know how you invoke NeoJSON right now, but you have to change that slightly.


Reply | Threaded
Open this post in threaded view
|

Re: json parse problem

Roelof
Op 16-3-2019 om 22:24 schreef Sven Van Caekenberghe:
> imageUrl1 := levelsJson detect: [ :item | item name = 'z4' ].
>    imageUrl := imageUrl1 tiles first url.
Thanks

I used NeoJsonReader. After chancing it to NeoJsonObject It worked.

I think I can do the same here :

json := NeoJSONReader fromString: (ZnEasy get: url) contents.
     artObjectJson := json at: #artObject.
     painter := ((artObjectJson at: #principalMakers) at: 1) at: #name.
     title := artObjectJson at: #title.

and here

(json at: #artObjects)
         do: [ :eachArtObject |
             | painting |
             painting := Painting new.
             painting objectNumber: (eachArtObject at: #objectNumber).
             paintings addPainting: painting ].
     ^ paintings

I will try that tomorrow. Now time to sleep here

Roelof