Json Jolt从属性到键值对的转换

问题描述

我正在尝试改变这样的东西:

BR <- cbind(BR,st_coordinates(st_centroid(BR)))

收件人:

ggplot() +
  geom_sf(data=BR,color="black",size=.1,show.legend = T) +
  geom_text(data = BR,aes(X,Y,label = abbrev_state),colour = "black") +
  geom_point(data=points,aes(x=x,y= y,color = types,size = types)) +
  scale_color_manual(values=c("#999999","#000000","#E69F00","#56B4E9","#3399FF")) +
  labs(x="Longitude",y="Latitude",color = "Types",size="Types") +
  geom_label_repel(data=points,label=obj)) +
  theme_bw()

因此,基本上将id保持不变,但将其余属性包装在一个json字符串中,该字符串将是一个键的值。这可能吗?谢谢

解决方法

是的,根据我的理解,您正在寻找的那个是可能的。下面是相同的用途。

它将以data作为输入,并保持Id不变。

let data = {
  Id: '123',Att1: 'value1',Att2: 'value2',Attn: 'valuen',};

const formatJSON = (data) => {
  const dataCopy = { ...data
  };
  const idVal = dataCopy.Id;
  delete dataCopy.Id;
  return {
    Id: idVal,AttJson: JSON.stringify(dataCopy)
  }
}

console.log(formatJSON(data))
console.log(data)
.as-console-wrapper {
  max-height: 100% !important;
}