1. Ludwin Feiten
  2. PowerBuilder
  3. Thursday, 4 May 2023 16:18 PM UTC

Hi everybody,

I have an issue with the JsonPackage Setvalue() function.
The help file says: "Sets the value for a key.  .... If the key already exists, then overwrite the value of this key".
To me that sounds like: If you set a value of a key in an loaded JSON file, the original key will be replaced by the string you pass in this function.

Is this wite?

I have this code:

JsonPackage    lnv_jpk
lnv_jpk = Create JsonPackage
lnv_jpk.ignorecase=true

long	ll_rc

string ls_json1, ls_json2, ls_json3, ls_rc

ls_json1 = '{"data":{"id":"012345-001","attributes":{}}}'
ls_json2 = '{"id":174,"fname":"Anabai","lname":"Zoblotny","address":"12600 N.E. Airport Way ","city":"Denver","state":"CO","zip":"80225","phone":"3035552757","company_name":"Hats Etc."}'

ls_rc = lnv_jpk.loadstring(ls_json1)
ll_rc = lnv_jpk.setvalue( "attributes", ls_json2, true)

ls_json3 = lnv_jpk.GetJsonString()

I would expect ls_json3 with one key "Attributes". Like this:

{
    "data": {
        "id": "012345-001",
        "attributes": {
            "id": 174,
            "fname": "Anabai",
            "lname": "Zoblotny",
            "address": "12600 N.E. Airport Way ",
            "city": "Denver",
            "state": "CO",
            "zip": "80225",
            "phone": "3035552757",
            "company_name": "Hats Etc."
        }
    }
}

But I get:

{
    "data": {
        "id": "012345-001",
        "attributes": {}
    },
    "attributes": {
        "id": 174,
        "fname": "Anabai",
        "lname": "Zoblotny",
        "address": "12600 N.E. Airport Way ",
        "city": "Denver",
        "state": "CO",
        "zip": "80225",
        "phone": "3035552757",
        "company_name": "Hats Etc."
    }
}

Do I miss s.th? Any suggestions?
(I can still go back and use PFC globalreplace() :-) )

 

 

Accepted Answer
René Ullrich Accepted Answer Pending Moderation
  1. Friday, 5 May 2023 04:43 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi Ludwin,

I think this is the same question: https://community.appeon.com/index.php/qna/q-a/how-to-edit-json-that-contains-nested-objects?limitstart=0#reply-38050

There you can find a workaround.

HTH,

René

Comment
There are no comments made yet.
Ludwin Feiten Accepted Answer Pending Moderation
  1. Friday, 5 May 2023 08:30 AM UTC
  2. PowerBuilder
  3. # 1

Hello René,
thank you for the hint.
Now I see the point:

SetValue () will work and replace the value of "key" but only if it is in a flat record.

That's poor coding and far from real-world problems. Very sad.
This new JSON-Objects are not a big help. I would say the opposite is true.
It takes too much time to figure out how the functions work.
I solve the problems much faster with simple string functions.

And the use of these obscure workarounds makes maintenance much more difficult.

Best regards, Ludwin

Comment
There are no comments made yet.
Arnd Schmidt Accepted Answer Pending Moderation
  1. Thursday, 4 May 2023 17:25 PM UTC
  2. PowerBuilder
  3. # 2

I used the JSONGenerator, but this can not modify existing items:-(

Instead it adds another "attributes" item.

JSONGenerator    lnv_jpk
lnv_jpk = Create JSONGenerator
long ll_ChildObject 
long	ll_rc

string ls_json1, ls_json2, ls_json3, ls_rc

ls_json1 = '{"data":{"id":"012345-001","attributes":{}}}'
ls_json2 = '{"id":174,"fname":"Anabai","lname":"Zoblotny","address":"12600 N.E. Airport Way ","city":"Denver","state":"CO","zip":"80225","phone":"3035552757","company_name":"Hats Etc."}'

lnv_jpk.importstring(ls_json1)

ll_ChildObject = lnv_jpk.GetItemByPath("/data")

ll_rc = lnv_jpk.importstring(ll_ChildObject, "attributes", ls_json2)

ls_json3 = lnv_jpk.GetJsonString()

 

The JSONPackage can not SetValue() for a path and does not a handle a path as key value like "/data/attributes".

 

I vote for: Go for a simple replace ;-)

Comment
There are no comments made yet.
Arnd Schmidt Accepted Answer Pending Moderation
  1. Thursday, 4 May 2023 16:23 PM UTC
  2. PowerBuilder
  3. # 3

Hi Ludwin,

 

I can see "data/attributes" vs "attributes" as the path.

Can you use https://docs.appeon.com/pb2022/powerscript_reference/GetItemByPath_func.html ?

hth

Arnd

Comment
  1. Ludwin Feiten
  2. Thursday, 4 May 2023 20:49 PM UTC
Thank you Arnd::

"I can see "data/attributes" vs "attributes" as the path."

That just adds an other key "data/attributes":

{"data":{"id":"012345-001","attributes":{}},"data/attributes":{"id":174,"fname":"Anabai","lname":"Zoblotny","address":"12600 N.E. Airport Way ","city":"Denver","state":"CO","zip":"80225","phone":"3035552757","company_name":"Hats Etc."}}
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.