Android Question Json.put replace old string

invocker

Active Member
Hellow everybody

I have a problem when send json string with patchstring the problem is that json.Put("Videos",CreateMap(videoname:videoUrl)) replace old string i need to append it not replace

my code is

json2:
Dim json As Map
    json.Initialize
    json.Put("Active",Active)
    json.Put("ID",ID)
    json.Put("UserName",UserName)
    json.Put("Country",Country1)
    json.Put("Img",Img)
    json.Put("Email",Email1)
    If videoUrl ="" Then
    Else
        json.Put("Videos",CreateMap(videoname:videoUrl)) 'probleme hier it replace old string
        
'        my json is :
'        Active :"ON"
'        Country    :""
'        Email:"myemail"
'        ID:"myid"
'        Img:"imglink"
'        UserName:"myname"
'        Videos
'        videos253998 :"videoslink" '  it replace this string hier
    End If
    Dim jgen As JSONGenerator
    jgen.Initialize(json)
    Log(jgen.ToPrettyString(2))
    Dim j As HttpJob : j.Initialize("",Me)
    j.PatchString(Urlpath,jgen.ToString)
 

teddybear

Well-Known Member
Licensed User
Hellow everybody

I have a problem when send json string with patchstring the problem is that json.Put("Videos",CreateMap(videoname:videoUrl)) replace old string i need to append it not replace
json.Put("Videos",CreateMap(videoname:videoUrl)), it overwrites the previous item with the key Videos.
CreateMap(videoname:videoUrl) just creates a new map not append.
if you want to append a new item in key videos, you need to get old items from old videos map and put them and a new item into a new map, such as json.put("videos", thenewmap) or json.put("videos", createmap(oldkeysldurls, ... videoname:videoUrl).
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
B4X:
    Dim Videos As List
    Videos.Initialize
   
    Videos.Add(CreateMap("videoname": "video Url 1"))
    Videos.Add(CreateMap("videoname": "video Url 2"))
    Videos.Add(CreateMap("videoname": "video Url 3"))
    Videos.Add(CreateMap("videoname": "video Url 4"))
   
    Dim Data As Map = CreateMap("Active" : 1, "ID":2, "UserName":3 , "Country" : 4, "Img" : 5, "Email" : 6, "videos" : Videos)
    Dim JSONGenerator1 As JSONGenerator
    JSONGenerator1.Initialize(Data)
    Log(JSONGenerator1.ToPrettyString(4))

log:
JSON:
{
    "Active": 1,
    "Img": 5,
    "UserName": 3,
    "Email": 6,
    "Country": 4,
    "videos": [
        {
            "videoname": "videoUrl 1"
        },
        {
            "videoname": "videoUrl 2"
        },
        {
            "videoname": "videoUrl 3"
        },
        {
            "videoname": "videoUrl 4"
        }
    ],
    "ID": 2
}

parse json:
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As Map = parser.NextObject
Dim Active As Int = jRoot.Get("Active")
Dim Img As Int = jRoot.Get("Img")
Dim UserName As Int = jRoot.Get("UserName")
Dim Email As Int = jRoot.Get("Email")
Dim Country As Int = jRoot.Get("Country")
Dim videos As List = jRoot.Get("videos")
For Each colvideos As Map In videos
 Dim videoname As String = colvideos.Get("videoname")
Next
Dim ID As Int = jRoot.Get("ID")

see:

 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Use "AS"
B4X:
    Dim Videos As List
    Videos.Initialize
    
    Videos.Add(CreateMap("videoname": "video Url 1"))
    Videos.Add(CreateMap("videoname": "video Url 2"))
    Videos.Add(CreateMap("videoname": "video Url 3"))
    Videos.Add(CreateMap("videoname": "video Url 4"))
    
    Dim Data As Map = CreateMap("Active" : 1, "ID":2, "UserName":3 , "Country" : 4, "Img" : 5, "Email" : 6, "videos" : Videos)
    Log(Data.As(JSON).ToString)
 
Upvote 0

invocker

Active Member
Thank you Mr omar The problem is when I send the string to database again it replace the text for example I have a json string in database like this

json3:
"videos": [
        {
            "videoname": "videoUrl 1"
        },
        {
            "videoname": "videoUrl 2"
        },
        {
            "videoname": "videoUrl 3"
        },
when I send again a json like this
B4X:
"videos": [
        {
            "videoname": "videoUrl 4"
        },
        {
            "videoname": "videoUrl 5"
        },
        {
            "videoname": "videoUrl 6"
        },

it replace the first json and not append it like this

B4X:
"videos": [
        {
            "videoname": "videoUrl 1"
        },
        {
            "videoname": "videoUrl 2"
        },
        {
            "videoname": "videoUrl 3"
        },
        {
            "videoname": "videoUrl 4"
        },
        {
            "videoname": "videoUrl 5"
        },
        {
            "videoname": "videoUrl 6"
        },
 
Upvote 0

invocker

Active Member
How is the actual json structure look like?
JSON:
{
  "uxkbwCtt7bZnsxcRtqw2QRHmmw32": {
    "Active": "ON",
    "Country": "mycountry",
    "Email": "myemail",
    "ID": "uxkbwCtt7bZnsxcRtqw2QRHmmw32",
    "Img": "https://..png",
    "UserName": "myname",
    "Videos": {
      "videos1215021": "https..mp4"
    }
  }
}
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Do you own the web service?
What is the documentation says?
Sometimes we send a :
POST - normally use to create a new record or override existing
PUT - normally use to update or override existing record

It is better refer to the documentation or if you are the one develop it then you need to determine how it handles the incoming json data.

Edit: I read again the first post and noticed that you mentioned about PATCH string. I never work with PATCH method and not very sure how the internal works. If I am not mistaken, when we PATCH, we only supply a partial of fields that need to be updated instead of mixture of non to-be-updated and to-be-updated fields.

 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Dear,
The group of videos is loaded into a variable of type list, https://www.b4x.com/android/help/collections.html#list
and you can add, delete, replace, etc. from this list and regenerate the json

B4X:
    Dim Data As Map = CreateMap("Active" : 1, "ID":2, "UserName":3 , "Country" : 4, "Img" : 5, "Email" : 6)
    Dim Videos As List
    Videos.Initialize
 
    Videos.Add(CreateMap("videoname": "video Url 1"))
    Videos.Add(CreateMap("videoname": "video Url 2"))
    Videos.Add(CreateMap("videoname": "video Url 3"))
    Videos.Add(CreateMap("videoname": "video Url 4"))
 
    Data.Put("videos", Videos)
    Log(Data.As(JSON).ToString)
 
 
    Videos.RemoveAt(0) 'delete video url 1
 
    Videos.Set(2, CreateMap("videoname": "video Url 4 change")) 'change video url 4
 
    Videos.Add(CreateMap("videoname": "video Url 6 new")) 'new items
 
    Data.Put("videos", Videos) 'Add List new
 
    Log(Data.As(JSON).ToString)

JSON:
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
{
    "Active": 1,
    "Img": 5,
    "UserName": 3,
    "Email": 6,
    "Country": 4,
    "videos": [
        {
            "videoname": "video Url 1"
        },
        {
            "videoname": "video Url 2"
        },
        {
            "videoname": "video Url 3"
        },
        {
            "videoname": "video Url 4"
        }
    ],
    "ID": 2
}
'''' NEW JSON
{
    "Active": 1,
    "Img": 5,
    "UserName": 3,
    "Email": 6,
    "Country": 4,
    "videos": [
        {
            "videoname": "video Url 2"
        },
        {
            "videoname": "video Url 3"
        },
        {
            "videoname": "video Url 4 change"
        },
        {
            "videoname": "video Url 6 new"
        }
    ],
    "ID": 2
}
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
The structure of the Json is the one that I publish or is published in another post??

 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
from what I see in your Json structure.
I wonder the following:

Do element keys have fixed names?
How many elements does the video group have?
Are group names duplicated?

 
Upvote 0

invocker

Active Member
Thank's for replay
Do element keys have fixed names?
Yes, all except for the video125 it is a random variable
How many elements does the video group have?
not fixed I use a video random value as key like this videos1524 for not replace it when send it with patchstring ,because want to append more videos

Are group names duplicated?
No
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
I have a problem when send json string with patchstring the problem is that json.Put("Videos",CreateMap(videoname:videoUrl)) replace old string i need to append it not replace
As a non-native English speaker, I wonder whether this recognizable somewhat unfortunate spelling means that the questioner actually means that he wants to add a url every time. If the questioner wants it, the json file is designed wrong. It should not work with a fix url field for each url, but with only one fix url field in a json array. Then you can add "endless" urls or you have to choose a fixed number of possible urls.

Most importantly, the statement "replace old string" says absolutely nothing about what should be done in which situation. It's all about When and How do you do What? Or elaborated: when do you do nothing (url = equal), when and how do you add (fix fields count or unlimited fields)?

Personally I think you need the following JSON file design:
JSON:
{
  "users": {
    "user": [
      {
        "ID": "1",
        "Active": "true",
        "Country": "GB",
        "UserName:": "Cruise",
        "Email:": "demo1@demo.com",
        "Img": "imglink",
        "Videos": [
          {
            "videoname": "videoUrl 1"
          },
          {
            "videoname": "videoUrl 2"
          },
          {
            "videoname": "videoUrl 3"
          },
          {
            "videoname": "videoUrl 4"
          },
          {
            "videoname": "videoUrl 5"
          },
          {
            "videoname": "videoUrl 6"
          }
        ]
      },
      {
        "ID": "2",
        "Active": "true",
        "Country": "GB",
        "UserName:": "Sharapova",
        "Email:": "demo2@demo.com",
        "Img": "imglink",
        "Videos": [
          {
            "videoname": "videoUrl 1"
          },
          {
            "videoname": "videoUrl 2"
          },
          {
            "videoname": "videoUrl 3"
          },
          {
            "videoname": "videoUrl 4"
          }
        ]
      },
      {
        "ID": "3",
        "Active": "false",
        "Country": "GB",
        "UserName:": "Downey Jr.",
        "Email:": "demo@demo3.com",
        "Img": "imglink",
        "Videos": [
          {
            "videoname": "videoUrl 1"
          },
          {
            "videoname": "videoUrl 2"
          },
          {
            "videoname": "videoUrl 3"
          }
        ]
      }
    ]
  }
}
Which can be create with:
Program code:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As Map = parser.NextObject
Dim users As Map = jRoot.Get("users")
Dim user As List = users.Get("user")
For Each coluser As Map In user
  Dim Active As String = coluser.Get("Active")
  Dim UserName: As String = coluser.Get("UserName:")
  Dim Email: As String = coluser.Get("Email:")
  Dim Img As String = coluser.Get("Img")
  Dim Country As String = coluser.Get("Country")
  Dim ID As String = coluser.Get("ID")
  Dim Videos As List = coluser.Get("Videos")
  For Each colVideos As Map In Videos
    Dim videoname As String = colVideos.Get("videoname")
  Next
Next
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…