JQ,将CSV(父子格式)转换为JSON,另一个问题 [英] JQ, convert CSV (parent child format) to JSON, another questions

查看:50
本文介绍了JQ,将CSV(父子格式)转换为JSON,另一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是另一篇文章的续篇: JQ,将CSV(父子格式)转换为JSON

您好,很抱歉再次询问.我试图获得以下格式,但没有成功.真的很感谢您的一些建议.我附上图片以显示其在层次结构视图中的外观以层次结构方式显示的图片,也许更容易.希望有可能吗?

*** CSV文件*****

<上一页> id,parent_id,大小 主题,空,1 分析,主题,1 聚类,分析,1 集聚集群,集群1 合并边缘,群集2 动画,主题,1 缓动,动画3 插值,动画,1 ArrayInterpolator,内插,4 RectangleInterpolator,interpolate,5 补间动画,动画6

这是我尝试实现的JSON文件.如果是父母(在下面有孩子),则仅显示ID.如果是孩子,请显示ID和大小.

**** JSON文件****

<上一页> { "ID":主题", 孩子们": [{ "ID":分析", 孩子们": [{ "ID":集群", 孩子们": [{ "ID":"Aggl,ome,rativeCluster", 大小":1 },{ "ID":"MergeEdge", 大小":2 }] }] },{ "ID":动画", 孩子们": [{ "ID":轻松", 大小":3 },{ "ID":插值", 孩子们": [{ "ID":"ArrayInterpolator", 大小":4 },{ "ID":"RectangleInterpolator", 大小":5 }] },{ "ID":"Tween", 大小":6 }] }] }

解决方案

以递归方式填写有关子级的详细信息很容易 使用递归函数(此处为closure/2)完成,编写该函数的效率很高.

def obj($headers):
  . as $in
  | reduce range(0; $headers|length) as $i ({}; 
      .[$headers[$i]] = $in[$i]);

# input:  either the id of an individual or 
#         an object representing an individual;
# output: the same individual but with .children and recursively
#         their children as an array of objects
def closure($dictionary; $children):
  def c: 
    if type == "string" then $dictionary[.] | c
    elif type=="object"
    then if has("children")
         then if (.children|length)>0 then .children = map(c) else . end
         elif $children[.id] then .children = ($children[.id] | map(c))
         else . end
    else . end;
  c;

split(",") as $headers
| [ inputs
    | split(",")
    | map_values(if . == "Null" then null else . end)
    | obj($headers) ]
| INDEX(.[]; .id) as $ids   # a dictionary mapping id => object
| (reduce .[] as $row ({};
      if $row.parent_id
      then .[$row.parent_id] += [$row.id]
      else . end ) ) as $children  # string => [ string ]
| map(closure($ids; $children) )
# tidy up:
| walk(if type=="object" 
       then if .children and (.children|length) > 0 
            then del(.size)
            else . end
       | del(.parent_id)
       else . end)

This is a continue of another post: JQ, convert CSV (parent child format) to JSON

Hi, sorry to ask again. I tried to get the following format, without success. Really appreciate for some advice. I attach a picture to show how it looks like in a hierarchy view a picture to show in a hierarchy way, maybe it is easier. Hope it is possible ?

*** CSV file *****

id,parent_id,size
Subject,Null,1
analytics,Subject,1
cluster,analytics,1
AgglomerativeCluster,cluster,1
MergeEdge,cluster,2
animate,Subject,1
Easing,animate,3
interpolate,animate,1
ArrayInterpolator,interpolate,4
RectangleInterpolator,interpolate,5
Tween,animate,6

Here is the JSON file which I tried to achieve. If it is a parent (has a child under), only show ID. If it is a child, then show ID and Size.

**** JSON file ****

{
    "ID": "Subject",
    "children": [{
        "ID": "analytics",
        "children": [{
            "ID": "cluster",
            "children": [{
                "ID": "Aggl,ome,rativeCluster",
                "size": 1
            }, {
                "ID": "MergeEdge",
                "size": 2
            }]
        }]
    }, {
        "ID": "animate",
        "children": [{
            "ID": "Easing",
            "size": 3
        }, {
            "ID": "interpolate",
            "children": [{
                "ID": "ArrayInterpolator",
                "size": 4
            }, {
                "ID": "RectangleInterpolator",
                "size": 5
            }]
        }, {
            "ID": "Tween",
            "size": 6
            }]
        }]
}

解决方案

To fill in the details about children recursively can most easily be accomplished using a recursive function -- here closure/2, which is written so as to be quite efficient.

def obj($headers):
  . as $in
  | reduce range(0; $headers|length) as $i ({}; 
      .[$headers[$i]] = $in[$i]);

# input:  either the id of an individual or 
#         an object representing an individual;
# output: the same individual but with .children and recursively
#         their children as an array of objects
def closure($dictionary; $children):
  def c: 
    if type == "string" then $dictionary[.] | c
    elif type=="object"
    then if has("children")
         then if (.children|length)>0 then .children = map(c) else . end
         elif $children[.id] then .children = ($children[.id] | map(c))
         else . end
    else . end;
  c;

split(",") as $headers
| [ inputs
    | split(",")
    | map_values(if . == "Null" then null else . end)
    | obj($headers) ]
| INDEX(.[]; .id) as $ids   # a dictionary mapping id => object
| (reduce .[] as $row ({};
      if $row.parent_id
      then .[$row.parent_id] += [$row.id]
      else . end ) ) as $children  # string => [ string ]
| map(closure($ids; $children) )
# tidy up:
| walk(if type=="object" 
       then if .children and (.children|length) > 0 
            then del(.size)
            else . end
       | del(.parent_id)
       else . end)

这篇关于JQ,将CSV(父子格式)转换为JSON,另一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆