akheron / jansson

C library for encoding, decoding and manipulating JSON data
http://www.digip.org/jansson/
Other
3.05k stars 811 forks source link

Updating json object #504

Closed ARao0123 closed 4 years ago

ARao0123 commented 4 years ago

I've the following content in my json input file (just part of file shown, it extend upto thousands of lines):

[ {
  "acvVersion" : "0.5"
}, {
  "vsId" : 6547,
  "algorithm" : "AES-CFB128",
  "isSample" : false,
  "testGroups" : [ {
    "tgId" : 1,
    "testType" : "AFT",
    "tests" : [ {
      "tcId" : 1,
      "iv" : "F34481EC3CC627BACD5DC3FB08F273E6",
      "pt" : "00000000000000000000000000000000",
      "key" : "00000000000000000000000000000000"
    }
     ],
    "direction" : "encrypt",
    "keyLen" : 128
  }
   ]
} ]

I've mocked up a small program to print the keys and values (following lines do all the job):

json = json_load_file("/home/json/janson/testvector-request.json", 0, &error);
print_json(json);

Output of this:

JSON Array of 2 elements:

JSON Object of 1 pair:
    JSON Key: "acvVersion"
    JSON String: "0.5"

JSON Object of 4 pairs:
    JSON Key: "vsId"
    JSON Integer: "6547"
    JSON Key: "algorithm"
    JSON String: "AES-CFB128"
    JSON Key: "isSample"
    JSON False
    JSON Key: "testGroups"
    JSON Array of 1 element:

JSON Object of 5 pairs:
        JSON Key: "tgId"
        JSON Integer: "1"
        JSON Key: "testType"
        JSON String: "AFT"
        JSON Key: "tests"
        JSON Array of 1 element:

JSON Object of 4 pairs:
            JSON Key: "tcId"
            JSON Integer: "1"
            JSON Key: "iv"
            JSON String: "F34481EC3CC627BACD5DC3FB08F273E6"
            JSON Key: "pt"
            JSON String: "00000000000000000000000000000000"
            JSON Key: "key"
            JSON String: "00000000000000000000000000000000"
        JSON Key: "direction"
        JSON String: "encrypt"
        JSON Key: "keyLen"
        JSON Integer: "128"

How can I add extra elements? I've tried "json_object_update_missing() and "json_object_update_existing() but may be I'm doing it wrong.

Here's what I want as my end result:

[ {
  "acvVersion" : "0.5"
}, {
  "vsId" : 6547,
  "algorithm" : "AES-CFB128",
  "isSample" : false,
  "testGroups" : [ {
    "tgId" : 1,
    "testType" : "AFT",
    "tests" : [ {
      "tcId" : 1,      
      "ct" : "xxxxxxxx"             <-- This is the only new addition, rest of the contents (except iv, pt, key) are same as input
    }
     ],
    "direction" : "encrypt",
    "keyLen" : 128
  }
   ]
} ]

Any suggestions on how to do it? I tried the following (json is the object containing my input):

// Scenario 1:
json = json_pack("{sisi}", "foo", 3, "baz", 4);
json_object_update_existing(json, json);
json_object_update_missing(json, json);

// Scenario 2:
other = json_pack("{sisi}", "foo", 3, "baz", 4);
json_object_update_existing(json, other);
json_object_update_missing(json, other);
AllenX2018 commented 4 years ago

Since I can't see your whole json input file, let's say your complete input looks like:

{
    "a": [{
        "acvVersion": "0.5"
    }, {
        "vsId": 6547,
        "algorithm": "AES-CFB128",
        "isSample": false,
        "testGroups": [{
            "tgId": 1,
            "testType": "AFT",
            "tests": [{
                "tcId": 1,
                "iv": "F34481EC3CC627BACD5DC3FB08F273E6",
                "pt": "00000000000000000000000000000000",
                "key": "00000000000000000000000000000000"
            }],
            "direction": "encrypt",
            "keyLen": 128
        }]
    }]
}

In this case, in order to get the result you mention in your description, you can try:

json_t *nested = json_array_get(json_object_get(json_array_get(
        json_object_get(json_array_get(json_object_get(json, "a"), 1), "testGroups"), 
        0), "tests"), 0);
json_object_del(nested, "iv");
json_object_del(nested, "pt");
json_object_del(nested, "key");
json_object_set_new(nested, "ct", json_string("xxxxxxxx"));

Here json is your input object. I don't think you need to call json_object_update to achieve your desired effect

ARao0123 commented 4 years ago

@AllenX2018 : Thanks for the comment, I will try it out. Here's an example input file : input

It does not contain the "a" that you've added at the beginning. How can I access the element if there's no "a" tag (as shown in input )?

AllenX2018 commented 4 years ago

Sorry for the late reply. The last comment I left was just an example. I checked your input file but I found that its content was different from your description at the begining. Could you describe which element you want to set/delete in your input file exactly?