skip to Main Content
{
  "field": "@timestamp",
  "value": "2024-08-20 23:00:13.426"
}
{
  "field": "@message",
  "value": "Started-1"
}
{
  "field": "@timestamp",
  "value": "2024-08-20 23:00:13.427"
}
{
  "field": "@message",
  "value": "Started-2"
}

Edited to show that the JSONs are repeating block

I have JSONs above which need to be merged as below. Please help

[{
  "@timestamp": "2024-08-20 23:00:13.426",
  "@message": "Started-1"
},
{
  "@timestamp": "2024-08-20 23:00:13.427",
  "@message": "Started-2"
}]

2

Answers


  1. file1.json
    
    {
      "field": "@timestamp",
      "value": "2024-08-20 23:00:13.426"
    }
    
    file2.json
    
    {
      "field": "@message",
      "value": "Started"
    }
    
    jq -s '.[0].value as $timestamp | .[1].value as $message | {"@timestamp": $timestamp, "@message": $message}' file1.json file2.json
    
    

    enter image description here

    Login or Signup to reply.
  2. You can use the from_entries function, which does exactly what you want, but it requires the future key’s name in a field called key or name. Thus, first transform the objects to meet this requirement (the value field can stay as-is):

    jq -s 'map({key: .field, value}) | from_entries'
    
    {
      "@timestamp": "2024-08-20 23:00:13.426",
      "@message": "Started"
    }
    

    Demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search