#Edit a json Entry

1 messages · Page 1 of 1 (latest)

graceful knotBOT
#

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

graceful knotBOT
#

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.

rustic badger
#

just wait for him, he will probably help you with that

#

but I think its quite early in the morning for him rn

nimble sentinel
#
import dev.mccue.json.Json;
import dev.mccue.json.JsonDecoder;

public class Main {
    public static void main(String[] args) {
        var j = Json.readString("{ \"name\"   : \"John Smith\", \"other\": 123} ");

        var j2 = Json.objectBuilder(JsonDecoder.object(j))
                .put("name", "John Doe")
                .build();

        System.out.println(j);
        System.out.println(j2);
    }
}
#

i think there is room for a better api probably

#

but copy it into a builder, replace properties

#

if you need to do something more involved then

#
import dev.mccue.json.Json;
import dev.mccue.json.JsonDecoder;

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        var j = Json.readString("{ \"name\"   : \"John Smith\", \"other\": 123} ");

        var modified = new HashMap<>(JsonDecoder.object(j));
        modified.put("name", Json.of("Joe Doe"));
        if (modified.containsKey("other")) {
            modified.put("apple", Json.of("what"));
        }

        var j2 = Json.of(modified);

        System.out.println(j);
        System.out.println(j2);
    }
}
#

you can go through normal collections

#

the JsonDecoder.object is just the same ol' "cast to JsonObject or throw"

#

API suggestions welcome - clearly some things that are easy in python are still hard here

#
j = json.loads(...)
j["a"]["b"] = 123
#

Another approach could be to round trip through an object

#
import dev.mccue.json.Json;
import dev.mccue.json.JsonDecoder;
import dev.mccue.json.JsonObject;

class Blob {
    String name;
    JsonObject other;
    
    static Blob fromJson(Json json) {
        var blob = new Blob();
        blob.name = JsonDecoder.field(json, "name", JsonDecoder::string);
        blob.other = JsonDecoder.object(json);
        return blob;
    }
    
    Json toJson() {
        return Json.objectBuilder(other)
                .put("name", name)
                .build();
    }
}

public class Main {
    public static void main(String[] args) {
        var j = Json.readString("{ \"name\"   : \"John Smith\", \"other\": 123} ");

        var blob = Blob.fromJson(j);
        blob.name = "Johnny Quest";
        
        var j2 = blob.toJson();

        System.out.println(j);
        System.out.println(j2);
    }
}
#

Depends what you want to do

#

do you want to just deal with that one k/v

#

or do you want to update one part of a larger document

#

can you give a more concrete example?

#

so its a big list like

#
[
  { "name"   : "One Smith"},
  { "name"   : "Two Smith"},
  { "name"   : "Red Smith"},
  { "name"   : "Blue Smith"}
]
#

how many keys are there

#

is the set known or not known

#

like is each thing a certain "shape"

#

like

#

name, age, height

#

okay

#

then honestly, reading into an object is the least annoying

#

read the json to some record or whatever, do your logic, then write it all back out

#

yeah there isn't a different way to work with a big json file

#
import dev.mccue.json.Json;
import dev.mccue.json.JsonDecoder;
import dev.mccue.json.JsonEncodable;
import dev.mccue.json.JsonObject;

import java.util.List;

record Person(String name, int age) implements JsonEncodable {
    public static Person fromJson(Json json) {
        return new Person(
                JsonDecoder.field(json, "name", JsonDecoder::string),
                JsonDecoder.field(json, "age", JsonDecoder::int_)
        );
    }

    @Override
    public Json toJson() {
        return Json.objectBuilder()
                .put("name", name)
                .put("age", age)
                .build();
    }

    Person withName(String name) {
        return new Person(name, this.age);
    }
}

public class Main {
    public static void main(String[] args) {
        var j = Json.readString("""
                [
                  { "name"   : "One Smith", "age": 20 },
                  { "name"   : "Two Smith", "age": 30 },
                  { "name"   : "Red Smith", "age": 25 },
                  { "name"   : "Blue Smith", "age": 40 }
                ]
                """);

        List<Person> people = JsonDecoder.array(j, Person::fromJson);
        
        List<Person> newPeople = people.stream()
                .map(person -> {
                    if (person.name().equals("Two Smith")) {
                        return person.withName("bob");
                    }
                    else {
                        return person;
                    }
                })
                .toList();
        
        Json j2 = Json.of(newPeople);

        System.out.println(j);
        System.out.println(j2);
    }
}

#

i'll add some examples to the readme

#

since im working on it anyways