我正在try 将给定的input.json转换为预期的output.json.我已成功地将该地址转换为字符串,但未能将业余爱好数组转换为所需的输出.

我try 了不同的方法,比如使用$通配符将键名称更改为name,但不起作用.我也使用@通配符将对象的名称从索引更改为某个名称,但也不起作用

Input.json

{
  "users": [
    {
      "name": "John Doe",
      "age": 35,
      "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": "12345"
      },
      "hobbies": [
        {
          "name": "reading",
          "type": "indoor"
        },
        {
          "name": "hiking",
          "type": "outdoor"
        }
      ]
    },
    {
      "name": "Jane Doe",
      "age": 30,
      "address": {
        "street": "456 First St",
        "city": "Othertown",
        "state": "NY",
        "zip": "67890"
      },
      "hobbies": [
        {
          "name": "swimming",
          "type": "outdoor"
        },
        {
          "name": "painting",
          "type": "indoor"
        }
      ]
    }
  ]
}

预期的Output.json

[  {  
    "Name": "John Doe",
    "Address": "123 Main St, Anytown, CA 12345", 
    "Hobbies": [ "reading", "hiking" ]
  },
  {
    "Name": "Jane Doe",
    "Address": "456 First St, Othertown, NY 67890",
    "Hobbies": [
      "swimming",
      "painting"
    ]
  }
]

推荐答案

您可以连续使用shiftmodify转换规范,例如

[
  { // form "Address" and "Hobbies" arrays
    "operation": "shift",
    "spec": {
      "users": {
        "*": {
          "name": "[#2].Name",
          "ad*": {
            "*": "[#3].ad1",//grab 1st asterisk replacement(might be multiple) after going 1 level up the tree 
            "state|zip": "[#3].ad2"
          },
          "h*": {
            "*": {
              "n*": "[#4].H&(2,1)"//grab 1st asterisk replacement after going 2 level up the tree
            }
          }
        }
      }
    }
  },
  { // concatenate the components of the "Address" array 
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "ad1": "=join(', ',@(1,&))",
        "ad2": "=join(' ',@(1,&))",
        "Adress": "=concat(@(1,ad1),', ',@(1,ad2))"
      }
    }
  },
  { // get rid of extra auxiliary attributes
    "operation": "remove",
    "spec": {
      "*": {
        "ad*": ""
      }
    }
  }
]

Edit:可以直接使用concat函数,如下您在 comments 部分中提到的:

[
  { 
    "operation": "shift",
    "spec": {
      "users": {
        "*": {
          "name": "[#2].Name",
          "ad*": {
            "*": "[#3].&"
          },
          "h*": {
            "*": {
              "n*": "[#4].H&(2,1)"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "Address": "=concat(@(1,street),', ',@(1,city),', ',@(1,state),' ',@(1,zip))"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "*t*|z*": ""
      }
    }
  }
]

但大体上,我更喜欢使用join而不是concat as much as possible作为join句柄.

Json相关问答推荐

无法根据vega规范中的条件设置文本 colored颜色

如何创建生成两个不同对象的JSON数组的SQL查询?

GO KaZaam转换返回意外结果

如何获取 JSON 对象字段值和同一 JSON 对象的下一个数组中的字段值?

Powershell解析JSON文件中的键或值

如何在 Apps 脚本中循环遍历 JSON 响应

每次在 SoapUI 中发送请求时,将 JSON 响应的子 node 分配给项目变量

将 JSON 解组为具有唯一元素的 map 切片

如何删除 django jsonfield 中的特定项目

JSON 模式验证

没有很多类的 GSON 解析

使用 JSON 的 javascript 深拷贝

jquery用json数据填充下拉列表

与classic 规范化表相比,postgres JSON 索引是否足够高效?

PHP json_encode json_decode UTF-8

MVC JsonResult camelCase 序列化

SCRIPT5009:JSON未定义

来自 Gson 的 JSON 字符串:删除双引号

从 JSON 到 JSONL 的 Python 转换

将对象序列化为 JSON 时循环引用检测到异常