Nerdy Drunk

Drunk on technology

User Tools

Site Tools


general:jq_merge_arrays

JQ Merge Arrays

$ SYSTEMSET1='{"Systems":[{"System":"sys-1"},{"System":"sys-2"}],"Options":[{"Option":"opt-1"},{"Options":"opt-2"}]}'
$ SYSTEMSET2='{"Systems":[{"System":"sys-3"},{"System":"sys-4"}],"Options":[{"Option":"opt-3"},{"Options":"opt-4"}]}'
$ SYSTEMSET3='{"Systems":[{"System":"sys-5"},{"System":"sys-6"}],"Options":[{"Option":"opt-5"},{"Options":"opt-6"}]}'
$ MERGEDSET=""
$ echo $SYSTEMSET1 | jq
{
  "Systems": [
    {
      "System": "sys-1"
    },
    {
      "System": "sys-2"
    }
  ],
  "Options": [
    {
      "Option": "opt-1"
    },
    {
      "Options": "opt-2"
    }
  ]
}
 
$ echo $SYSTEMSET2 | jq
{
  "Systems": [
    {
      "System": "sys-3"
    },
    {
      "System": "sys-4"
    }
  ],
  "Options": [
    {
      "Option": "opt-3"
    },
    {
      "Options": "opt-4"
    }
  ]
}
 
$ echo $SYSTEMSET3 | jq
{
  "Systems": [
    {
      "System": "sys-5"
    },
    {
      "System": "sys-6"
    }
  ],
  "Options": [
    {
      "Option": "opt-5"
    },
    {
      "Options": "opt-6"
    }
  ]
}
$ echo $SYSTEMSET1 $SYSTEMSET2 | jq -s '.[0].Systems=[.[].Systems[]]|.[0]'
{
  "Systems": [
    {
      "System": "sys-1"
    },
    {
      "System": "sys-2"
    },
    {
      "System": "sys-3"
    },
    {
      "System": "sys-4"
    }
  ],
  "Options": [
    {
      "Option": "opt-1"
    },
    {
      "Options": "opt-2"
    }
  ]
}
 
$ MERGEDSET=$(echo $SYSTEMSET1 $SYSTEMSET2 | jq -s '.[0].Systems=[.[].Systems[]]|.[0]')
$ echo $MERGEDSET $SYSTEMSET3 | jq -s '.[0].Systems=[.[].Systems[]]|.[0]'
{
  "Systems": [
    {
      "System": "sys-1"
    },
    {
      "System": "sys-2"
    },
    {
      "System": "sys-3"
    },
    {
      "System": "sys-4"
    },
    {
      "System": "sys-5"
    },
    {
      "System": "sys-6"
    }
  ],
  "Options": [
    {
      "Option": "opt-1"
    },
    {
      "Options": "opt-2"
    }
  ]
}
 
$ MERGEDSET=$(echo $MERGEDSET $SYSTEMSET3 | jq -s '.[0].Systems=[.[].Systems[]]|.[0]')
$ echo $MERGEDSET $SYSTEMSET2 | jq -s '.[0].Options=[.[].Options[]]|.[0]'
{
  "Systems": [
    {
      "System": "sys-1"
    },
    {
      "System": "sys-2"
    },
    {
      "System": "sys-3"
    },
    {
      "System": "sys-4"
    },
    {
      "System": "sys-5"
    },
    {
      "System": "sys-6"
    }
  ],
  "Options": [
    {
      "Option": "opt-1"
    },
    {
      "Options": "opt-2"
    },
    {
      "Option": "opt-3"
    },
    {
      "Options": "opt-4"
    }
  ]
}
 
$ MERGEDSET=$(echo $MERGEDSET $SYSTEMSET2 | jq -s '.[0].Options=[.[].Options[]]|.[0]')
$ echo $MERGEDSET $SYSTEMSET3 | jq -s '.[0].Options=[.[].Options[]]|.[0]'
{
  "Systems": [
    {
      "System": "sys-1"
    },
    {
      "System": "sys-2"
    },
    {
      "System": "sys-3"
    },
    {
      "System": "sys-4"
    },
    {
      "System": "sys-5"
    },
    {
      "System": "sys-6"
    }
  ],
  "Options": [
    {
      "Option": "opt-1"
    },
    {
      "Options": "opt-2"
    },
    {
      "Option": "opt-3"
    },
    {
      "Options": "opt-4"
    },
    {
      "Option": "opt-5"
    },
    {
      "Options": "opt-6"
    }
  ]
}
 
$ MERGEDSET=$(echo $MERGEDSET $SYSTEMSET3 | jq -s '.[0].Options=[.[].Options[]]|.[0]')
general/jq_merge_arrays.txt · Last modified: 2022/07/21 10:41 by 127.0.0.1