Skip to content

How to do JSON parsing and subsequent type annotation within a nested array #4288

Answered by vitoyucepi
mojikosu asked this question in Q&A
Discussion options

You must be logged in to vote

I'd like to replace

# Find the first section with non-null metadata, using the `default` argument
let first_section = list.hd(
  default={
    metadata = [],
    tabname = "",
    type = "",
    url = null
  },
  sections
)
let metadata_list = 
  if first_section.metadata != null() then
    first_section.metadata
  else
    []
  end

#mdata = list.map(fun(x) -> (x.title, x.text), metadata_list)

with

metadata_list =
  try
    metadata_section = list.find(fun(section) -> section.metadata != null(), sections)
    list.map(
      fun(metadata_item) -> (metadata_item.title, metadata_item.text),
      null.get(metadata_section.metadata)
    )
  catch err do
    []
  end

print(metadata_list)

Replies: 2 comments 13 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
13 replies
@mojikosu
Comment options

@vitoyucepi
Comment options

@mojikosu
Comment options

@vitoyucepi
Comment options

Answer selected by mojikosu
@mojikosu
Comment options

@mojikosu
Comment options

@vitoyucepi
Comment options

@mojikosu
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants