#Edit a json Object

1 messages · Page 1 of 1 (latest)

heavy kiteBOT
#

<@&987246399047479336> please have a look, thanks.

heavy kiteBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

west stump
#

what have you got so far?:

#

have you loaded the json file?

#

ok and now what do you want to do with that HashMap?

#

why do you even have a HashMap then?

#

why are you putting the content of the Json into a HashMap and not stick to the returned Json from Json.read?

#

?

#

cant you tell me the reason for the HashMap?

#

ok and you updating the values and now want to writ ethe content of the HashMap back to the file?

#

in the json or in the hashmap?

#

then you need to write it to the file back

#

couldnt find a way to just edit one line

#

in his tutorial

#

but just wait for him ig

#

you already pinged him

#

and I think its night time for him

#

I think thats because if writing in a file to java you would always need to write the whole file and cant edit just a single line without writing the old content as well

#

it was smth like that

#

yeah, opening for writing takes the longest time anyways

#

So writing one line or ten shouldn’t really matter at this scale

jagged star
#

Format the code next time

west stump
tidal dock
#

yes

#

write the whole hash map back out

#

yep

#

edit the hashmap

#

okay so you have your hashmap<String, String[]>

west stump
#

you need to get it to a Json object again

tidal dock
#
HashMap<String, String[]> m = ...;

var array = m.get("example");
if (array != null) {
    array[3] = "2";
}
#

so that will edit the array in your hashmap

#

(the way you are describing it)

#

then just write it back out

#

override your json with the HashMap
Hm?

west stump
#

the write methods accepts an Json object

tidal dock
#

so how to go from HashMap<String, String[]> -> Json?

west stump
#

^

tidal dock
#

(have to acknowledge that HashMap<String, String[]> is a bit cookoo as an application model)

west stump
#

btw cant you edit the content in the Json object directly?

west stump
tidal dock
#

well - do you have a write method already?

#

can you show it

#

I said write

#

not read

#

okay

#

so use Json.objectBuilder() and Json.arrayBuilder()

#

loop over your hashmap, and for each entry add to the builders

west stump
#

dont you have a of method for map?

tidal dock
#

if they have a method to at least encode a String[] that will work

#

but doing the normal loop I think is what they should do

tidal dock
#

just to talk about a small thing - json, as a data format, is especially poorly suited to "update just one part without writing the whole thing out again"

#

i'm sure its doable in some way

#

but in general your mode of interaction is going to be "load thing into memory", followed by "edit it", then "write out again"

west stump
#

check the github/javadocs ig

tidal dock
#

a JsonEncodable is an interface which contains a toJson method

tidal dock
#

Use Json.arrayBuilder() and a for loop

#

if you can write a method that just does String[] -> Json we can make use of that to do the whole hashMap

#

okay

#

focus on this

#
static Json stringArrayToJson(String[] strings) {

}
#

Use Json.arrayBuilder() and a for loop

tidal dock
#

This is a bad habit

#

Test your code

#

It's close

#

You need to use an object builder

#

But please don't ask if something is good when you yourself have not tried running it

tidal dock
#

@atomic kernel hmm wait

#

ObjectBuilder isn't public

#

use JsonObject.Builder and Json.objectBuilder()

#

not ObjectBuilder directly

#

notice how ObjectBuilder is in the internal package?

#

yeah