Reform a Json-Post on RUST

I am new at rust and trying to change a post-statement, but it doesn't work.
Would be nice if someone could help me out.

actual code:

let sw_optionen = response["included"]
                    .as_array()
                    .unwrap()
                    .into_iter()
                    .filter(|&x| x["type"].as_str() == Some("property_group_option"))
                    .map(|x| x["id"].as_str() )
                    .collect::<Vec<_>>();
                if sw_optionen.len() > 0 {
                    ops.insert(format!("10-delete-options-{}", f_produkt.id), json!({
                        "entity": "product",
                        "action": "delete",
                        "payload": [{
                            "id": shop_id,
                            "optionIds": sw_optionen
                        }],
                    }));
                }

Post looks like:

  "10-delete-options-105074867": {
    "action": "delete",
    "entity": "product",
    "payload": [
      {
        "id": "830d1d89d48eafeb04a08a5a84fc47e7",
        "optionIds": [
          "2c696615ae56ca5c4d8f5f24ccec3e5a",
          "7419bcabd82831d08913a4145efacff1",
          "a5f68318a731c85c11b82d6d4a0f302b"
        ]
      }
    ]
  },
  "10-delete-options-240298": {
    "action": "delete",
    "entity": "product",
    "payload": [
      {
        "id": "f1ee7e507a23c350d5e68f08b2ceeee8",
        "optionIds": [
          "2c696615ae56ca5c4d8f5f24ccec3e5a",
          "7e6cc32ab2a8bcf5ba3dbc70031d2207",
          "97085da39dab960d2a166482b2ec30e0"
        ]
      }
    ]
  },

I am trying to change the code that the post looks like:

  "10-delete-options-105074867": {
    "action": "delete",
    "entity": "product_options",
    "payload": [
      {"productId": "830d1d89d48eafeb04a08a5a84fc47e7", "optionIds": "2c696615ae56ca5c4d8f5f24ccec3e5a"},
      {"productId": "830d1d89d48eafeb04a08a5a84fc47e7", "optionIds": "a5f68318a731c85c11b82d6d4a0f302b"}
    ]
  },
  "10-delete-options-240298": {
    "action": "delete",
    "entity": "product",
    "payload": [
      {"id": "f1ee7e507a23c350d5e68f08b2ceeee8","optionIds": "2c696615ae56ca5c4d8f5f24ccec3e5a"},
      {"id": "f1ee7e507a23c350d5e68f08b2ceeee8","optionIds":"7e6cc32ab2a8bcf5ba3dbc70031d2207"},
      {"id": "f1ee7e507a23c350d5e68f08b2ceeee8","optionIds":"97085da39dab960d2a166482b2ec30e0"}
    ]
  },

I guess this should not be hard to change, but i am trying 2 Days now and not getting to the result i want. Pls help =)

What have you tried so far?

Is the only difference how it is formatted? I don't think it's easy to have it formatted like that.

Sadly i didn't noticed my tries. Here are some i remember:

The following 2 maping the whole getOption into the optionID. Like: attributes, mediaId, groupId ...)

let sw_optionen = response["included"]
                    .as_array()
                    .unwrap()
                    .into_iter()
                    .filter(|&x| x["type"].as_str() == Some("property_group_option"))
                    .map(|x| json!({"optionId": x}) )
                    .collect::<Vec<_>>();
                if sw_optionen.len() > 0 {
                    ops.insert(format!("10-delete-options-{}", fis_produkt.id), json!({
                        "entity": "product_option",
                        "action": "delete",
                        "payload": [{
                            "productId": shopware_id,
                            "optionId": sw_optionen
                        }],
                    }));
                }

let sw_optionen = response["included"]
                    .as_array()
                    .unwrap()
                    .into_iter()
                    .filter(|&x| x["type"].as_str() == Some("property_group_option"))
                    .map(|x| json!({"id": x}) )
                    .collect::<Vec<_>>();
                if sw_optionen.len() > 0 {
                    ops.insert(format!("10-delete-options-{}", fis_produkt.id), json!({
                        "entity": "product_option",
                        "action": "delete",
                        "payload": [{
                            "productId": shopware_id,
                            "optionId": sw_optionen
                        }],
                    }));
                }

Simple try just change names:

let sw_optionen = response["included"]
                    .as_array()
                    .unwrap()
                    .into_iter()
                    .filter(|&x| x["type"].as_str() == Some("property_group_option"))
                    .map(|x| x["id"].as_str() )
                    .collect::<Vec<_>>();
                if sw_optionen.len() > 0 {
                    ops.insert(format!("10-delete-options-{}", fis_produkt.id), json!({
                        "entity": "product_option",
                        "action": "delete",
                        "payload": [{
                            "productId": shopware_id,
                            "optionId": sw_optionen
                        }],
                    }));
                }

Output:

  "10-delete-options-105136060": {
    "action": "delete",
    "entity": "product_option",
    "payload": [
      {
        "optionId": [
          "67190a1c83adc9799e0a79db53576d50"
        ],
        "productId": "1f125d4aefdc9633cdf7d0843da81c72"
      }
    ]
  },

Here square brackets on optionId are wrong and if more then one optionId is past it will add it after the first with a "," what is wrong too.

This are some of my tries. Hope it helps =)

Yea it's only about the output format. It need to be like:
"10-delete-options-f_produkt.id" : {
"action": "delete",
"entity": "product_options",
"payload":[
{"productId: "XXX", "optionId": "XXX"},
{"productId: "XXX", "optionId": "XXX"}
]}

I found a solution while testing :smiley:
Solution:

let sw_optionen = response["included"]
                    .as_array()
                    .unwrap()
                    .into_iter()
                    .filter(|&x| x["type"].as_str() == Some("property_group_option"))
                    .map(|x| json!({
                        "productId": shop_id,
                        "optionId": x["id"].as_str(),
                    }))
                    .collect::<Vec<_>>();
                if sw_optionen.len() > 0 {
                    ops.insert(format!("10-delete-options-{}", f_produkt.id), json!({
                        "entity": "product_option",
                        "action": "delete",
                        "payload": sw_optionen,
                    }));
                }

Thanks for the help :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.