用 python 格式化 JSON

Format JSON with python

我想以这种形式发送 api 请求:

         "line_items": [
        {

            "account_id": "1717893000000067010",
            "debit_or_credit": "debit",
            "amount": 400,
            "tags": [
                {
                    "tag_option_id": "1717893000000115007",
                    "tag_id": "1717893000000000333"
                },
                {
                    "tag_option_id": "1717893000000123007",
                    "tag_id": "1717893000000000335"
                },
                {
                    "tag_option_id": "1717893000000126003",
                    "tag_id": "1717893000000000337"
                }
            ]

上面的JSON可能有几百个(line_items),其中每个(标签)可能有不同数量的词典。

我在 python 中所做的是:

          accounts = []
          tags = []
          for line in payroll.line_ids:

            ######## code missing some correction for tags

            if len(line.x_zoho_jtag) == 0:
               the_tags = {"tag_id": " ", "tag_option_id": " "}
               tags.append(the_tags)


            for tag in line.x_zoho_jtag:
                for option in line.x_zoho_jtag_option:
                    if option.tag_ids == tag.tag_id:
                      the_tags = {"tag_id": tag.tag_id, "tag_option_id": option.option_tag_id}
                      tags.append(the_tags)

              ########

            if line.debit != 0.0:
               credit = {"amount": line.debit,"account_id": line.x_zoho_account_no,"debit_or_credit": "debit", "tags": tags}
               accounts.append(credit)
               print(credit)
            else:
                debit = {"amount": line.credit, "account_id": line.x_zoho_account_no,"debit_or_credit": "credit", "tags": tags}
                accounts.append(debit)
                print(debit)
          print(accounts)

正如您在上面的 python 代码中看到的,我有 2 个列表(帐户和标签)。我将 (account_id, debit_or_credit, amount) 存储在 (accounts) 列表中,它工作正常。

  if line.debit != 0.0:
           credit = {"amount": line.debit,"account_id": line.x_zoho_account_no,"debit_or_credit": "debit", "tags": tags}
           accounts.append(credit)
           print(credit)
        else:
            debit = {"amount": line.credit, "account_id": line.x_zoho_account_no,"debit_or_credit": "credit", "tags": tags}
            accounts.append(debit)
            print(debit)

除此之外,我还添加了 (tags) 键和 (tags) 列表,如您在上一行中所见。

我面临的问题是我需要在列表中传递多个字典块的 (tags) 键。那么该怎么做呢?

预期输出:

  "line_items": [       
          {
"account_id": "1717893000000067010",
"debit_or_credit": "debit",
"amount": 400,
"tags": [
  {
    "tag_option_id": " ",
    "tag_id": " "
  },
  {
    "tag_option_id": "1717893000000126003",
    "tag_id": "1717893000000000337"
  },
  {
    "tag_option_id": "1717893000000123007",
    "tag_id": "1717893000000000335"
  }


   "line_items": [
       {
"account_id": "1717893000000067036",
"debit_or_credit": "credit",
 "amount": 400,
"tags": [
  {
    "tag_option_id": "1717893000000126003",
    "tag_id": "1717893000000000337"
  }

错误的输出:

            {
"account_id": "1717893000000067010",
"debit_or_credit": "debit",
"amount": 400,
"tags": [
  {
    "tag_option_id": " ",
    "tag_id": " "
  },
  {
    "tag_option_id": "1717893000000126003",
    "tag_id": "1717893000000000337"
  },
  {
    "tag_option_id": "1717893000000123007",
    "tag_id": "1717893000000000335"
  }


      {
"account_id": "1717893000000067036",
"debit_or_credit": "credit",
 "amount": 400,
"tags": [
  {
    "tag_option_id": " ",
    "tag_id": " "
  },
  {
    "tag_option_id": "1717893000000126003",
    "tag_id": "1717893000000000337"
  },
  {
    "tag_option_id": "1717893000000123007",
    "tag_id": "1717893000000000335"
  }

我想我会见到你...很快...电影标签 = [] 在顶层 for 循环中。

这里的问题是,当您遍历标签时,您没有区分应该放在 credit 上的标签和应该放在 debit 上的标签。
您需要做的是先获取该行,然后获取该行的关联标签。
我认为下面应该可以工作,但有一些重复,因此可以进一步改进。

    accounts = []
    for line in payroll.line_ids:
        if line.debit != 0.0:
            credit = {
                "amount": line.debit,
                "account_id": line.x_zoho_account_no,
                "debit_or_credit": "debit", 
                "tags": []
            }
            if len(line.x_zoho_jtag) == 0:
                credit["tags"].append({"tag_id": " ", "tag_option_id": " "})
            else:
                for tag in line.x_zoho_jtag:
                    for option in line.x_zoho_jtag_option:
                        if option.tag_ids == tag.tag_id:
                          credit["tags"].append({"tag_id": tag.tag_id, 
                                                 "tag_option_id": option.option_tag_id})
            accounts.append(credit)
            print(credit)
        else:
            debit = {
                "amount": line.credit, 
                "account_id": line.x_zoho_account_no,
                "debit_or_credit": "credit", 
                "tags": []
            }
            if len(line.x_zoho_jtag) == 0:
                debit["tags"].append({"tag_id": " ", "tag_option_id": " "})
            else:
                for tag in line.x_zoho_jtag:
                    for option in line.x_zoho_jtag_option:
                        if option.tag_ids == tag.tag_id:
                          debit["tags"].append({"tag_id": tag.tag_id, 
                                                "tag_option_id": option.option_tag_id})

            accounts.append(debit)
            print(debit)

进一步重构

将重复的代码块移动到一个函数中

accounts = []
for line in payroll.line_ids:
    if line.debit != 0.0:
        credit = create_account("credit", line)
        accounts.append(credit)
        print(credit)
    else:
        debit = create_account("debit", line)
        accounts.append(debit)
        print(debit)


def create_account(account_type, line):
    if account_type == "credit":
        amount = line.debit
        d_or_c = "debit"
    else: 
        amount = line.credit
        d_or_c = "credit"

    account = {
        "amount": amount,
        "account_id": line.x_zoho_account_no,
        "debit_or_credit": d_or_c, 
        "tags": []
    }
    if len(line.x_zoho_jtag) == 0:
        account["tags"].append({"tag_id": " ", "tag_option_id": " "})
    else:
        for tag in line.x_zoho_jtag:
            for option in line.x_zoho_jtag_option:
                if option.tag_ids == tag.tag_id:
                  account["tags"].append({"tag_id": tag.tag_id, 
                                         "tag_option_id": option.option_tag_id})
    return account